DataGridCheckBoxColumn loses IsReadOnly state when applying ElementStyle

无人久伴 提交于 2019-12-30 19:52:26

问题


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.

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>

回答1:


When you set ElementStyle on the DataGridCheckBoxColumn you should include FrameworkElement.IsHitTestVisible="False" to your Style:

<Setter Property="FrameworkElement.IsHitTestVisible" Value="False"/>

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>



回答2:


I have a suspicion that this is a bug (as it doesn't happen on other column types).

To work around it, all you need to do is make sure you base your style on the default style; by adding BasedOn="{x:Static DataGridCheckBoxColumn.DefaultElementStyle}" to the Style element:

<DataGrid IsReadOnly="True">
    <DataGrid.Columns>
        <DataGridCheckBoxColumn Header="Test" IsReadOnly="True" Binding="{Binding MyBinding}">
            <DataGridCheckBoxColumn.ElementStyle>
                <Style BasedOn="{x:Static DataGridCheckBoxColumn.DefaultElementStyle}">
                    <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>

This approach has the advantage that you don't have to do lots of work if you are intending on changing the DataGrid.IsReadOnly property at runtime.



来源:https://stackoverflow.com/questions/37584559/datagridcheckboxcolumn-loses-isreadonly-state-when-applying-elementstyle

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