0

问候,

我有一个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;
}

在此处输入图像描述

我如何选择组
Thanks 中的所有行。

4

2 回答 2

0

This code correctly:

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
        {
        }
 }
于 2013-11-13T16:44:10.860 回答
0

我看不到你绑定了哪个对象DataGrid.ItemsSource,但我会假设它是一个集合。假设您在此集合中的数据项已实现INotifyPropertyChanged接口(以便 UI 将更新),您可以在您的Click, 或ICommand处理程序中执行此操作:

for (int index = 0; index < YourCollection.Count; index++)
{
    YourCollection[index].BoolPropertyBoundToCheckBox = true;
}
于 2013-11-08T15:55:56.523 回答