[英]WPF Toolkit Datagrid - how do you turn selection off?
I have a datagrid in WPF that I am binding to an object. 我在WPF中有一个数据网格,我绑定到一个对象。
I have a DataGridCheckBoxColumn on there which I want the users to be able to go through and tick the ones they want. 我有一个DataGridCheckBoxColumn,我希望用户能够通过并勾选他们想要的那些。 Problem is they have to click twice, once for selection then again to check/uncheck. 问题是他们必须点击两次,一次选择然后再次检查/取消选中。 How on earth do you turn this off, I've been searching for way to long to find the answer to this. 你究竟怎么把它关掉,我一直在寻找方法来寻找答案。 The datagrid has SelectionMode and SelectionUnit properties - neither of which accept 'none' or 'go away' datagrid具有SelectionMode和SelectionUnit属性 - 两者都不接受'none'或'go away'
Any help is appreciated! 任何帮助表示赞赏! My code is below for reference 我的代码在下面供参考
<my:DataGrid Margin="15" Name="dgPreview"
AutoGenerateColumns="False" CanUserSortColumns="True"
CanUserDeleteRows="True"
Background="White"
ColumnHeaderHeight="20"
VerticalScrollBarVisibility="Visible"
RowDetailsVisibilityMode="Visible"
>
<my:DataGrid.Columns>
<my:DataGridCheckBoxColumn MinWidth="50" Width="Auto" Header="Include" Binding="{Binding Include}" />
<my:DataGridTextColumn MinWidth="50" Width="Auto" Header="Override #" Binding="{Binding OverrideNumber}" />
<my:DataGridTextColumn MinWidth="220" Width="*" Header="Name" Binding="{Binding Name}" />
<my:DataGridTextColumn MinWidth="50" Width="Auto" IsReadOnly="True" Header="Preview" Binding="{Binding Preview}" />
</my:DataGrid.Columns>
</my:DataGrid>
The first click puts the cell in edit mode then the second click allows you to modify the checkbox. 第一次单击将单元格置于编辑模式,然后第二次单击允许您修改复选框。 You can change this behavior by using a DataGridTemplateColumn instead of a DataGridCheckBoxColumn. 您可以使用DataGridTemplateColumn而不是DataGridCheckBoxColumn来更改此行为。 Replace your DataGridCheckBoxColumn with this: 用以下内容替换DataGridCheckBoxColumn:
<my:DataGridTemplateColumn MinWidth="50" Width="Auto" Header="Include" SortMemberPath="Include">
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Style="{StaticResource DataGridCheckBoxStyle}" IsChecked="{Binding Path=Include}" />
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>
DataGridCheckBoxStyle just makes the CheckBox look a little nicer in the DataGrid: DataGridCheckBoxStyle只是让CheckBox在DataGrid中看起来更好一些:
<Style x:Key="DataGridCheckBoxStyle" TargetType="CheckBox" BasedOn="{StaticResource {x:Type CheckBox}}">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Margin" Value="8,0,3,0" />
</Style>
First of, I know this is a pretty old question but I still thought I'd try and answer it. 首先,我知道这是一个非常古老的问题,但我仍然认为我会尝试回答它。
I had the same problem a couple of days ago and came across a surprisingly short solution for it (see this blog ). 几天前我遇到了同样的问题,并且遇到了一个令人惊讶的简短解决方案(参见此博客 )。 Basically, all you need to do is replace the DataGridCheckBoxColumn
definition in your XAML with the following: 基本上,您需要做的就是使用以下内容替换XAML中的DataGridCheckBoxColumn
定义:
<DataGridTemplateColumn Header="MyCheckBoxColumnHeader">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox HorizontalAlignment="Center" VerticalAlignment="Center" IsChecked="{Binding Path=MyViewModelProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
The upside of this solution is obvious - it's XAML-only; 这个解决方案的优点是显而易见的 - 它只是XAML; thus it effectively refrains your from burdening your code-back with additional UI logic and helps you maintain your status in the eyes of MVVM zealots ;). 因此,它可以有效地避免您使用额外的UI逻辑来增加您的代码负担,并帮助您在MVVM狂热者的眼中保持您的状态;)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.