wpf DataGridCheckBoxColumn 的单击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/759779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Click event for DataGridCheckBoxColumn
提问by Prashant Cholachagudda
I have a DataGrid
in a WPF form with a DataGridCheckBoxColumn
, but I did not find any click event, Checked and unchecked for it...
我有一个DataGrid
带有 的 WPF 表单DataGridCheckBoxColumn
,但我没有找到任何点击事件,已选中和未选中它...
Are these events available for the DataGridCheckBoxColumn
? If not please suggest some workaround I could use.
这些事件可用于DataGridCheckBoxColumn
? 如果没有,请提出一些我可以使用的解决方法。
回答by IFink
Quoted from William Han's answer here: http://social.msdn.microsoft.com/Forums/ar/wpf/thread/9e3cb8bc-a860-44e7-b4da-5c8b8d40126d
引用 William Han 的回答:http: //social.msdn.microsoft.com/Forums/ar/wpf/thread/9e3cb8bc-a860-44e7-b4da-5c8b8d40126d
It simply adds an event to the column. It is a good simple solution.
它只是向列添加一个事件。这是一个很好的简单解决方案。
Perhaps you can use
EventSetter
as example below:Markup:
<Window x:Class="DataGridCheckBoxColumnTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:DataGridCheckBoxColumnTest" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <local:People x:Key="People"/> </Window.Resources> <Grid> <DataGrid ItemsSource="{StaticResource People}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding Path=Name}" Header="Name"/> <DataGridCheckBoxColumn Binding="{Binding Path=LikeCar}" Header="LikeCar"> <DataGridCheckBoxColumn.CellStyle> <Style> <EventSetter Event="CheckBox.Checked" Handler="OnChecked"/> </Style> </DataGridCheckBoxColumn.CellStyle> </DataGridCheckBoxColumn> </DataGrid.Columns> </DataGrid> </Grid> </Window>
Code:
using System; using System.Windows; namespace DataGridCheckBoxColumnTest { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } void OnChecked(object sender, RoutedEventArgs e) { throw new NotImplementedException(); } } } namespace DataGridCheckBoxColumnTest { public class Person { public Person(string name, bool likeCar) { Name = name; LikeCar = likeCar; } public string Name { set; get; } public bool LikeCar { set; get; } } } using System.Collections.Generic; namespace DataGridCheckBoxColumnTest { public class People : List<Person> { public People() { Add(new Person("Tom", false)); Add(new Person("Jen", false)); } } }
也许您可以使用
EventSetter
以下示例:标记:
<Window x:Class="DataGridCheckBoxColumnTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:DataGridCheckBoxColumnTest" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <local:People x:Key="People"/> </Window.Resources> <Grid> <DataGrid ItemsSource="{StaticResource People}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding Path=Name}" Header="Name"/> <DataGridCheckBoxColumn Binding="{Binding Path=LikeCar}" Header="LikeCar"> <DataGridCheckBoxColumn.CellStyle> <Style> <EventSetter Event="CheckBox.Checked" Handler="OnChecked"/> </Style> </DataGridCheckBoxColumn.CellStyle> </DataGridCheckBoxColumn> </DataGrid.Columns> </DataGrid> </Grid> </Window>
代码:
using System; using System.Windows; namespace DataGridCheckBoxColumnTest { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } void OnChecked(object sender, RoutedEventArgs e) { throw new NotImplementedException(); } } } namespace DataGridCheckBoxColumnTest { public class Person { public Person(string name, bool likeCar) { Name = name; LikeCar = likeCar; } public string Name { set; get; } public bool LikeCar { set; get; } } } using System.Collections.Generic; namespace DataGridCheckBoxColumnTest { public class People : List<Person> { public People() { Add(new Person("Tom", false)); Add(new Person("Jen", false)); } } }
回答by Lance Cleveland
Expanding on the DataGridCell concept noted above, this is what we used to get it working.
扩展上面提到的 DataGridCell 概念,这就是我们用来让它工作的东西。
...XAML...
... XAML ...
<DataGrid Grid.ColumnSpan="2" Name="dgMissingNames" ItemsSource="{Binding Path=TheMissingChildren}" Style="{StaticResource NameListGrid}" SelectionChanged="DataGrid_SelectionChanged">
<DataGrid.Columns>
<DataGridTemplateColumn CellStyle="{StaticResource NameListCol}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=Checked, UpdateSourceTrigger=PropertyChanged}" Name="theCheckbox" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding Path=SKU}" Header="Album" />
<DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" "/>
<DataGridTextColumn Binding="{Binding Path=Pronunciation}" Header="Pronunciation" />
</DataGrid.Columns>
</DataGrid>
TheMissingChildren is an ObservableCollection object that contains the list of data elements including a boolean field "Checked" that we use to populate the datagrid.
TheMissingChildren 是一个 ObservableCollection 对象,它包含数据元素列表,包括我们用来填充数据网格的布尔字段“Checked”。
The SelectionChanged code here will set the checked boolean in the underlying TheMissingChildren object and fire off a refresh of the items list. That ensures that the box will get checked off & display the new state no matter where you click on the row. Clicking the checkbox or somewhere in the row will toggle the check on/off.
此处的 SelectionChanged 代码将在底层 TheMissingChildren 对象中设置已检查的布尔值,并触发项目列表的刷新。这可确保无论您单击行的何处,该框都将被选中并显示新状态。单击复选框或行中的某处将打开/关闭检查。
private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGrid ThisGrid = (DataGrid)sender;
CheckedMusicFile ThisMusicfile = (CheckedMusicFile)ThisGrid.SelectedItem;
ThisMusicfile.Checked = !ThisMusicfile.Checked;
ThisGrid.Items.Refresh();
}
回答by William
How about something like this.
这样的事情怎么样。
partial class SomeAwesomeCollectionItems : INotifyPropertyChanged
{
public event PropertyChanged;
protected void OnPropertyChanged(string property)
{
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(property);
}
private bool _IsSelected;
public bool IsSelected { get { return _IsSelected; } set { _IsSelected = Value; OnPropertyChanged("IsSelected"); } }
}
Then in XAML
然后在 XAML 中
<DataGrid ItemsSource="{Binding Path=SomeAwesomeCollection"} SelectionMode="Single">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridRow}"
BasedOn="{StaticResource {x:Type DataGridRow}}">
<!--Note that you will probably need to base on other style if you have stylized your DataGridRow-->
<Setter Property="IsSelected" Value="{Binding Path=IsSelected, UpdateSourceTrigger=PropertyChanged}" />
</Style>
</DataGrid.Resources
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding Path=IsSelected, UpdateSourceTrigger=PropertyChanged}" />
<!--More Columns-->
</DataGrid.Columns>
</DataGrid>
One note with this approach, however, is you may run into issues with virtualization and checked items not clearing (not sure, haven't tested with SelectionMode="Single"). If that is the case, the simplest workaround I have found to work is to turn virtualization off, but perhaps there is a better way to get around that particular issue.
但是,这种方法的一个注意事项是,您可能会遇到虚拟化问题,并且检查项目未清除(不确定,尚未使用 SelectionMode="Single" 进行测试)。如果是这种情况,我发现可行的最简单的解决方法是关闭虚拟化,但也许有更好的方法来解决该特定问题。
回答by Kent Boogaart
<wpf:DataGridCheckBoxColumn Header="Cool?" Width="40" Binding="{Binding IsCool}"/>
回答by Longurimont
If you do not want to add the event to your style you can also do it this way.
如果您不想将事件添加到您的样式中,您也可以这样做。
<DataGridCheckBoxColumn x:Name="name" Header="name?" Binding="{Path=Name}"
<DataGridCheckBoxColumn.CellStyle>
<Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
<EventSetter Event="CheckBox.Checked" Handler="Checked"/>
</Style>
</DataGridCheckBoxColumn.CellStyle>
</DataGridCheckBoxColumn>eckBoxColumn.CellStyle>
</DataGridCheckBoxColumn>