简体   繁体   English

WPF Toolkit Datagrid - 如何关闭选择?

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

相关问题 如何在WPF工具箱数据网格控件中设置水平网格线的粗细? - How do you set the thickness of the horizontal gridlines in the WPF toolkit datagrid control? 您如何在C#中以编程方式为WPF工具包datagrid创建列? - How do you create columns programmatically in C# for a WPF toolkit datagrid? 当数据网格获得焦点时,如何关闭WPF XCeed DataGridControl并始终选择第一个单元格? - How do I turn off the WPF XCeed DataGridControl alway selecting the first cell when the datagrid gets focus? WPF工具包DataGrid如何设置列标题SelectionBackground? - WPF Toolkit DataGrid how do I set Column Header SelectionBackground? WPF DataGrid:如何在DataGrid中进行迭代以获取行和列? - WPF DataGrid: How do you iterate in a DataGrid to get rows and columns? 如何在我的工具箱Datagrid中对齐行选择? - How to align row selection in My toolkit Datagrid? 如何遍历WPF工具包Datagrid的行 - How to loop over the rows of a WPF toolkit Datagrid 如何在C#中打开和关闭系列 - How do you turn a series on and off in C# 如何在 VS 2013 中关闭 Razor 突出显示? - How do you turn off Razor highlighting in VS 2013? 如何在WPF工具包datagrid“过滤器扩展”中保存过滤器值? - How do I save filter values in WPF toolkit datagrid “filter extension”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM