【问题标题】:DataGridCheckBoxColumn loses IsReadOnly state when applying ElementStyle应用 ElementStyle 时 DataGridCheckBoxColumn 失去 IsReadOnly 状态
【发布时间】:2016-06-02 06:46:11
【问题描述】:

我需要将DataGridCheckBoxColumn 垂直居中。由于我没有在DataGridCheckBoxColumn 中找到属性,所以我申请了ElementStyle。但是,当应用此样式时,我的CheckBox 再次变为可检查,尽管它在我的DataGrid 中设置为ReadOnly(整个DatagridReadOnly)和DataGridCheckBoxColumn 本身。

如何创建一个垂直居中的CheckBox 以保持其ReadOnly 状态?这是我的代码:

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

【问题讨论】:

    标签: c# wpf datagrid readonly elementstyle


    【解决方案1】:

    当您在DataGridCheckBoxColumn 上设置ElementStyle 时,您应该将FrameworkElement.IsHitTestVisible="False" 包含在您的Style 中:

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

    此外,如果您将TargetType="CheckBox" 添加到Style,那么您不必再为每个Setter 重复FrameworkElement

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

    【讨论】:

    • 谢谢,这行得通,但我仍然不明白为什么它是必要的。改变样式真的会导致只读属性丢失吗?
    • 您可能还应该将Focusable 设置为False,因此不能使用键盘进行编辑。
    【解决方案2】:

    我怀疑这是一个错误(因为它不会发生在其他列类型上)。

    要解决这个问题,您需要做的就是确保您的样式基于默认样式;通过将BasedOn="{x:Static DataGridCheckBoxColumn.DefaultElementStyle}" 添加到Style 元素:

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

    这种方法的优点是,如果您打算在运行时更改 DataGrid.IsReadOnly 属性,则无需做大量工作。

    【讨论】:

      猜你喜欢
      • 2021-07-08
      • 2013-06-15
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-21
      相关资源
      最近更新 更多