WPF Toolkit Datagrid - 如何关闭选择?

发布于 2024-07-30 17:56:49 字数 1175 浏览 3 评论 0原文

我在 WPF 中有一个数据网格,我将其绑定到一个对象。

我在那里有一个 DataGridCheckBoxColumn,我希望用户能够浏览并勾选他们想要的。 问题是他们必须单击两次,一次进行选择,然后再次进行选中/取消选中。 你到底如何关闭这个功能,我一直在寻找这个问题的答案。 数据网格具有 SelectionMode 和 SelectionUnit 属性 - 两者都不接受“无”或“离开”

任何帮助表示赞赏! 我的代码如下供参考

<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>

I have a datagrid in WPF that I am binding to an object.

I have a DataGridCheckBoxColumn on there which I want the users to be able to go through and tick the ones they want. 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'

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖树树初阳… 2024-08-06 17:56:50

第一次单击将单元格置于编辑模式,然后第二次单击允许您修改复选框。 您可以使用 DataGridTemplateColumn 而不是 DataGridCheckBoxColumn 来更改此行为。 将 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 只是使 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>

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. Replace your DataGridCheckBoxColumn with this:

<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:

<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>
櫻之舞 2024-08-06 17:56:50

首先,我知道这是一个很老的问题,但我仍然认为我会尝试回答它。

几天前我遇到了同样的问题,并遇到了一个令人惊讶的简短解决方案(请参阅此博客)。 基本上,您需要做的就是将 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>

此解决方案的优点是显而易见的 - 它仅适用于 XAML; 因此,它可以有效地避免您用额外的 UI 逻辑给代码带来负担,并帮助您保持在 MVVM 狂热者眼中的地位;)。

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:

<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; 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 ;).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文