SyntaxHighlighter

2021-02-12

DataGridCheckBoxColumn動作模式變更

DataGridCheckBoxColumn裡的CheckBox,在預設狀態下,必須先點一次,Row取得focurs之後才能改變狀態。使用上不是很直覺,跟一般的CheckBox不太一樣。

背後的原因是因為DataGridCell本身有分成顯示用的ElementStyle跟編輯用的EditElementStyle兩種模式,為了做出區分,犧牲了CheckBox的直覺性。

可以依靠設定Style的方法,使操作更為直覺。

修改方式如下:

在各自的Style設定中(可以放在上位DataGrid的DataGrid.Resource裡),加入以下程式碼
<Style TargetType="CheckBox" x:Key="CheckBoxCellStyle" />

或是
<Style TargetType="CheckBox" x:Key="CellCheckBoxStyle"
       BasedOn="{StaticResource {x:Type CheckBox}}" />

然後就可以在DataGridCheckBoxColumn上設定我們所需的ElementStyle
<DataGridCheckBoxColumn Binding="{Binding Selected}"
                        ElementStyle="{StaticResource CheckBoxCellStyle}" />

做完以上的設定,指定的DataGridCheckBoxColumn裡面的CheckBox就會是我們常用的單點就會有反應的CheckBox了。

以上。

人気の投稿