问题描述
我需要垂直居中一个 DataGridCheckBoxColumn
。由于我没有在 DataGridCheckBoxColumn
中找到一个Property,所以我应用了一个 ElementStyle
。但是,当应用此样式时,我的 CheckBox
可以再次检查,虽然它设置为 ReadOnly
在我的 DataGrid
(整个 Datagrid
是 ReadOnly
),而在code> DataGridCheckBoxColumn 本身。
I need to vertically center a DataGridCheckBoxColumn
. Since I did not find a Property inside DataGridCheckBoxColumn
, I applied an ElementStyle
. However, when this style is applied, my CheckBox
becomes checkable again, although it is set to ReadOnly
in my DataGrid
(the whole Datagrid
is ReadOnly
), and in DataGridCheckBoxColumn
itself.
如何创建一个垂直居中的 CheckBox
保持其 ReadOnly
状态?这是我的代码:
How can I create a vertically centered CheckBox
that keeps its ReadOnly
state? Here is my code:
<DataGrid IsReadOnly="True">
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="Test" IsReadOnly="True" Binding="{Binding MyBinding}">
<DataGridCheckBoxColumn.ElementStyle>
<Style>
<Setter Property="FrameworkElement.Margin" Value="0,1,0,0" />
<Setter Property="FrameworkElement.VerticalAlignment" Value="Center" />
<Setter Property="FrameworkElement.HorizontalAlignment" Value="Center" />
</Style>
</DataGridCheckBoxColumn.ElementStyle>
</DataGridCheckBoxColumn>
</DataGrid.Columns>
</DataGrid>
推荐答案
设置 ElementStyle
在 DataGridCheckBoxColumn
你应该包括到您的样式
:
<Setter Property="FrameworkElement.IsHitTestVisible" Value="False"/>
另外如果添加 TargetType =CheckBox
到 Style
,那么您不必为每个 Setter
重复 FrameworkElement
code>更多:
Also if you add TargetType="CheckBox"
to the Style
then you don't have to repeat FrameworkElement
for each Setter
anymore:
<DataGridCheckBoxColumn.ElementStyle>
<Style TargetType="CheckBox">
<Setter Property="Margin" Value="0,1,0,0" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="IsHitTestVisible" Value="False"/>
</Style>
</DataGridCheckBoxColumn.ElementStyle>
这篇关于DataGridCheckBoxColumn在应用ElementStyle时失去IsReadOnly状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!