[英]DataGrid with a checkbox column to allow the user to select rows?
问候,
我有一个带复选框列的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;
}
如何选择组中的所有行
谢谢 。
我看不到您已绑定到DataGrid.ItemsSource
对象,但我将假定它是一个集合。 假设此集合中的数据项已实现INotifyPropertyChanged
接口(以便UI会更新),则可以在Click
或ICommand
处理程序中执行以下操作:
for (int index = 0; index < YourCollection.Count; index++)
{
YourCollection[index].BoolPropertyBoundToCheckBox = true;
}
此代码正确:
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
{
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.