[英]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.