Data grid checkbox automation

十年热恋 提交于 2019-12-22 14:16:44

问题


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>

回答1:


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>

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



来源:https://stackoverflow.com/questions/1493958/data-grid-checkbox-automation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!