数据网格复选框自动化 [英] Data grid checkbox automation

查看:117
本文介绍了数据网格复选框自动化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有复选框的datagrid。当我单击单元格时,我想要复选框,当我选择包含它的单元格时自动选中。现在的做法是,我需要选择单元格,然后点击复选框,这是非常讨厌我们。

I have a datagrid with a check box. When I click n the cell, I would like the checkbox to be automatically checked when I selected the cell which contains it. what it's doing right now is that I need to select the cell then click on the checkbox and that is quite annoying for us.

<dg:DataGrid Name="GridUsureOperation" Margin="10,444,82,6" ItemsSource="{Binding}" Style="{StaticResource GridMenu}" SelectionMode="Single" SelectionUnit="Cell">
    <dg:DataGrid.Columns>
        <dg:DataGridTextColumn Header="Opération" Width="*" MinWidth="60" 
                               Binding="{Binding Operation.DescOperation}" 
                               IsReadOnly="True" />
        <dg:DataGridTextColumn Header="Dernière maintenance" Width="SizeToHeader" MinWidth="50"
                               Binding="{Binding DateDerniereMaintenance, StringFormat=yyyy-MM-dd}" 
                               IsReadOnly="True"/>
        <dg:DataGridTextColumn Header="Usure dernière maintenance" Width="SizeToHeader" MinWidth="50"
                               Binding="{Binding UsureDerniereOperation}" 
                               IsReadOnly="True"/>
        <dg:DataGridTextColumn Header="Fréquence(usure)" Width="SizeToHeader" MinWidth="50"
                               Binding="{Binding QteUsure}" 
                               IsReadOnly="True"/>
        <dg:DataGridTextColumn Header="Unité" Width="SizeToHeader" MinWidth="50"
                               Binding="{Binding TypeUsure.Description}" 
                               IsReadOnly="True"/>
        <dg:DataGridCheckBoxColumn Header="Forcer?" Width="SizeToHeader" MinWidth="50"
                               Binding="{Binding AfficherMaintenance}"                                         
                               IsReadOnly="False">

        </dg:DataGridCheckBoxColumn>
    </dg:DataGrid.Columns>
</dg:DataGrid>


推荐答案

我不喜欢使用DataGridCheckBoxColumn,有焦点之前,你可以与复选框进行交互。所以我把一个复选框放在一个模板列,你只需要一个单击来改变状态。

I don't like using the DataGridCheckBoxColumn because it needs to have focus before you can interact with the checkbox. So I instead put a CheckBox inside a template column and you only need one click to change state.

<dg:DataGridTemplateColumn Width="SizeToHeader">
    <dg:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding Path=Selected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="2,0,2,0" />
        </DataTemplate>
    </dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>

但是,如果你真的想要复选框在单元格中的任何位置改变状态只是在复选框),你可以使用下面的代码,这使得CheckBox控件占据单元格的整个大小。

However, if you actually want the checkbox to change state when they click anywhere in the cell (and not just on the checkbox) you can use the following code, which makes the CheckBox control take up the entire size of the cell.

<dg:DataGridTemplateColumn Width="SizeToHeader">
    <dg:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding Path=Selected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="2,0,2,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
        </DataTemplate>
    </dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>

UPDATE
我没有做过WPF开发一会儿。我有一个问题,使用这个解决方案现在,DataGrid被编入到WPF库,不再需要工具包。复选框不想更新以前写的backing属性,所以我添加了一个更完整的绑定语句,似乎工作。

UPDATE I haven't done WPF dev in quite a while. I was having an issue using this solution now that the DataGrid is baked into the WPF libraries and you don't need the toolkit anymore. The checkbox did not want to update the backing property as previously written, so I have added a more complete binding statement that does seem to work.

这篇关于数据网格复选框自动化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆