简体   繁体   English

以编程方式禁用在 xaml 中创建的样式触发器

[英]Disabling style triggers created in xaml programmatically

I am working on ac# WPF project which I am using a style trigger to style the background colour of the each row based on one of the cell values.我正在研究 ac# WPF 项目,我正在使用样式触发器根据单元格值之一设置每一行的背景颜色的样式。

Below is my style trigger.下面是我的风格触发器。

<Window.Resources>
        <Style TargetType="DataGridRow">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Highest Alarm Level}" Value="Critical">
                    <Setter Property="Background" Value="Red" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Highest Alarm Level}" Value="Medium">
                    <Setter Property="Background" Value="Orange" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Highest Alarm Level}" Value="Warning">
                    <Setter Property="Background" Value="Yellow" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Highest Alarm Level}" Value="Info">
                    <Setter Property="Background" Value="White" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

What I need to be able to do, is under certain circumstances I don't want this style to be used, so what I am after, is if in the C# code a certain condition becomes to true, I want to disable all of the styling done above to not be used, ie all of the styling is turned off so the background colour of the rows are not set.我需要做的是,在某些情况下我不希望使用这种样式,所以我所追求的是,​​如果在 C# 代码中某个条件变为真,我想禁用所有不使用上面完成的样式,即所有样式都已关闭,因此未设置行的背景颜色。

Add a Key to your custom DataGridRow Style为您的自定义DataGridRow Style添加一个键

<Style TargetType="DataGridRow" x:Key="MyRowStyle">
    <!-- Define Triggers -->
</Style>

Then you just need to switch between default and custom styles.然后你只需要在默认和自定义样式之间切换。

If you set Style with null , that means the default Style will be applied如果您将Style设置为null ,则表示将应用默认Style

dataGrid.RowStyle = _boolCondition ? this.FindResource("MyRowStyle") as Style : null;

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

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