DataGrid有一个复选框列,允许用户选择行?

时间:2013-11-08 15:38:05

标签: c# wpf datagrid groupstyle

此致

我有DataGrid个复选框列,允许用户选择行,列标题中的“全选”复选框,以及组标题中的“全选”复选框。

XAML:

<DataGrid CanUserAddRows="True"  CanUserDeleteRows="True" Grid.ColumnSpan="2"     Margin="7,4,8,41" Name="customerDataGrid" Grid.Row="3" ItemsSource="{Binding}"  ColumnHeaderStyle="{Binding Source={StaticResource TabControlInnerBorder}}" DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid,  AncestorLevel=1}}">
    <DataGrid.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Path=City}" FontWeight="Bold" Padding="3"/>
                    </StackPanel>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                <Expander>
                                    <Expander.Header>
                                        <StackPanel Orientation="Horizontal">
                                            <TextBlock  Text="{Binding Path=Name}"   />
                                            <TextBlock  Text="{Binding Path=ItemCount}" />
                                            <TextBlock  Text="Emp(s)" />
                                            <CheckBox  Content="{Binding Path=Name}" ClickMode="Press" Checked="CheckBox_Checked"  />
                                        </StackPanel>
                                    </Expander.Header>
                                    <ItemsPresenter />
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                   </Setter>
               </Style>
           </GroupStyle.ContainerStyle>
       </GroupStyle>
   </DataGrid.GroupStyle>
</DataGrid>

.CS

 private ICollectionView defaultView1;
 public main1_windows()
 {
     InitializeComponent();
     List<Employ> empl = new List<Employ>();
     empl = LoadData();
     this.defaultView1 = CollectionViewSource.GetDefaultView(empl);
     this.defaultView1.GroupDescriptions.Add(new PropertyGroupDescription  ("dep"));
     this.defaultView1.Filter = new Predicate<object>(Contains1);  
 }

public bool Contains1(object de)
{
    Employ order = de as Employ;
    //Return members whose Orders have not been filled 
    return (order.Name.ToString().Contains(textBox4.Text).Equals(true));
}

private List<Employ> LoadData()
{
    List<Employ> employ = new List<Employ>();
    employ.Add(new Employ()
        {
            ID = 2000,
            Name = "moh",
            DOB = new DateTime(1985, 5, 15),
            dep="pro",
            IsNew = false 
        });
    employ.Add(new Employ()
        {
            ID = 3000,
            Name = "jac",
            DOB = new DateTime(1985, 5, 15),
            dep="pro",
            IsNew = false 
        });
    employ.Add(new Employ()
        {
            ID = 4000,
            Name = "ahmad",
            DOB = new DateTime(1985, 5, 15),
            dep="admin",
             IsNew = false 
        });
    return employ;
}
public class Employ
{
    public int ID { get; set; }
    public string Name { get; set; }
    public DateTime DOB { get; set; }
    public string dep { get; set; } 
    public bool IsNew { get; set; }
}

private void textBox4_TextChanged(object sender, TextChangedEventArgs e)
{
    this.defaultView1.Filter = new Predicate<object>(Contains1);
}

private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
    // I want when the apical true check box in the group all the rows in this group check box true value;
}

enter image description here

如何选择组中的所有行
谢谢。

2 个答案:

答案 0 :(得分:0)

我无法看到你绑定到DataGrid.ItemsSource的对象,但我会假设它是一个集合。假设此集合中的数据项已实现INotifyPropertyChanged界面(以便UI更新),您可以在ClickICommand处理程序中执行此操作:

for (int index = 0; index < YourCollection.Count; index++)
{
    YourCollection[index].BoolPropertyBoundToCheckBox = true;
}

答案 1 :(得分:0)

此代码正确:

private void CheckBox_Checked(object sender, RoutedEventArgs e)
    {
        CheckBox chc = sender as CheckBox;
        try
        {
            foreach (var item in mEmployees)
            {
                if (item.Dep.ToString() == chc.Content.ToString())
                {
                    item.IsSelected = true;
                    MessageBox.Show(chc.Content.ToString());

                }

            }
        }
        catch
        {
        }
 }