简体   繁体   English

禁用控件WPF的DataGridTemplateColumn样式

[英]DataGridTemplateColumn Styling for Disabled Controls WPF

I have a DataGrid control in my WPF application, and I have a checkbox column as one of the columns. 我的WPF应用程序中有一个DataGrid控件,并且有一个复选框列作为其中的一列。 I am using a DataGridTemplate to build the data grid. 我正在使用DataGridTemplate来构建数据网格。 My goal is to change the background color of any checkbox data grid cell where the checkbox is disabled. 我的目标是更改禁用了复选框的任何复选框数据网格单元的背景色。 My checkbox column looks like this: 我的复选框列如下所示:

<!-- style for the checkbox column cells -->
<Style x:Key="DefaultCheckboxCellStyle" TargetType="DataGridCell">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Grid Background="{TemplateBinding Background}">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="True">
            <Setter Property="Background" Value="HotPink" />
        </Trigger>
    </Style.Triggers>
</Style>

...
<DataGridTemplateColumn Header="MyCheckBoxColumn" CellStyle="{StaticResource DefaultCheckboxCellStyle}">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox Name="chk" IsChecked="{Binding MyChkChecked, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding Path=MyChkEnabled}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
...

Nothing I am doing seems to change the cell background for disabled checkboxes. 我在做什么似乎都没有更改禁用复选框的单元格背景。 I am displaying disabled checkboxes, but all of the cells themselves are still enabled. 我正在显示禁用的复选框,但是所有单元格本身仍处于启用状态。 Is there a way to bind the enabled value from the inner checkbox control to the cell? 有没有办法将内部复选框控件中的启用值绑定到单元格? If so, I could use that in my trigger. 如果是这样,我可以在触发器中使用它。

Many thanks! 非常感谢!

Disabling the checkboxes, as you've, found, won't disable the cells. 如您所知,禁用复选框不会禁用单元格。 There are a couple of ways to go about this. 有两种方法可以解决此问题。

One is to have the Style trigger bind to the same viewmodel property that disables the checkbox. 一种是将样式触发器绑定到禁用复选框的同一viewmodel属性。 This belongs in DefaultCheckboxCellStyle , replacing the one trigger you have. 这属于DefaultCheckboxCellStyle ,替换了您拥有的一个触发器。

<Style.Triggers>
    <DataTriger Binding="{Binding MyChkEnabled}" Value="True">
        <Setter Property="Background" Value="HotPink" />
    </DataTriger>
</Style.Triggers>

Or you could try this in DefaultCheckboxCellStyle : Keep the trigger you have, and disable the actual cell. 或者,您可以在DefaultCheckboxCellStyle尝试此操作:保留触发器,并禁用实际的单元格。 You could then omit the other XAML that disables the checkbox itself, as it's a child of the cell anyway. 然后,您可以忽略另一个禁用复选框本身的XAML,因为它还是该单元格的子级。

<Setter Property="IsEnabled" Value="{Binding MyChkEnabled}" />
<Style.Triggers>
    <Trigger Property="IsEnabled" Value="True">
        <Setter Property="Background" Value="HotPink" />
    </Trigger>
</Style.Triggers>

UpdateSourceTrigger=PropertyChanged is a no-op there, by the way. 顺便说一句, UpdateSourceTrigger=PropertyChanged在那儿是没有操作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM