简体   繁体   English

WPF没有满足DataTrigger值时设置属性

[英]Setting Property when No DataTrigger Values are Satisfied WPF

I am VERY new to WPF AND C#, so there may be a much better way to accomplish what I am attempting. 我对WPF和C#非常陌生,因此可能会有更好的方法来完成我正在尝试的工作。 Therefore, I am open to other methods. 因此,我愿意接受其他方法。

As far as what I've got, I am trying to program a Digital VMB (Visual Management Board) for the maintenance department where I work. 据我所知,我正在尝试为我工作的维护部门编程一个数字VMB(可视化管理委员会)。 They want a section to display the number of days our plant has gone without an accident: "Days Safe". 他们想要一个部分来显示我们的工厂没有发生意外的天数:“安全天数”。 I successfully have the binding set for this TextBlock: 我已成功为此TextBlock设置了绑定:

<TextBlock Text="{Binding DaysSafe}" FontSize="60"  FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center" >

I have a series of DataTriggers to change the Foreground color of the text to a certain color, based on the value of the text. 我有一系列DataTriggers,可根据文本的值将文本的前景颜色更改为某种颜色。 Basically, I want the DaysSafe text to be: Red when 0 Orange when 1 and so on (you can see the colors in my code below): 基本上,我希望DaysSafe文本为:0时为红色,1时为橙色,依此类推(您可以在下面的代码中看到颜色):

<StackPanel VerticalAlignment="Center" >
            <TextBlock Text="{Binding DaysSafe}" FontSize="60"  FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center" >
                <TextBlock.Style>
                    <Style TargetType="{x:Type TextBlock}">
                        <Style.Triggers >
                            <DataTrigger Binding="{Binding DaysSafe}" Value="0">
                                <Setter Property="Foreground" Value="Red"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding DaysSafe}" Value="1">
                                <Setter Property="Foreground" Value="OrangeRed" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding DaysSafe}" Value="2">
                                <Setter Property="Foreground" Value="Orange" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding DaysSafe}" Value="3">
                                <Setter Property="Foreground" Value="Yellow" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding DaysSafe}" Value="4">
                                <Setter Property="Foreground" Value="Yellow" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding DaysSafe}" Value="5">
                                <Setter Property="Foreground" Value="GreenYellow" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>
        </StackPanel>

This functions correctly, the colors change when the days are lower. 此功能可以正常运行,当天数减少时,颜色也会改变。 HOWEVER, I would like for the foreground color to "Default" to green when the value of DaysSafe is over 5. Right now, any value above 5 is black when I want it to be green. 但是,当DaysSafe的值超过5时,我希望将前景色“默认”设置为绿色。现在,当我希望它为绿色时,任何大于5的值都是黑色。 I tried adding the foreground="green" attribute in the first TextBlock section, but this overrides the DataTriggers and the foreground is always green. 我尝试在第一个TextBlock部分中添加frontant =“ green”属性,但这将覆盖DataTriggers,并且前景始终为绿色。

Any help with how I may create a default value for the foreground? 关于如何为前景创建默认值的任何帮助? Thanks. 谢谢。

You could just add a setter that sets the default Foreground of the TextBlock to Green: 您可以添加一个设置器,将TextBlock的默认前景设置为绿色:

<Style TargetType="{x:Type TextBlock}">
    <Setter Property="Foreground" Value="Green" />
    <Style.Triggers >
        <DataTrigger Binding="{Binding DaysSafe}" Value="0">
            <Setter Property="Foreground" Value="Red"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding DaysSafe}" Value="1">
            <Setter Property="Foreground" Value="OrangeRed" />
        </DataTrigger>
        <DataTrigger Binding="{Binding DaysSafe}" Value="2">
            <Setter Property="Foreground" Value="Orange" />
        </DataTrigger>
        <DataTrigger Binding="{Binding DaysSafe}" Value="3">
            <Setter Property="Foreground" Value="Yellow" />
        </DataTrigger>
        <DataTrigger Binding="{Binding DaysSafe}" Value="4">
            <Setter Property="Foreground" Value="Yellow" />
        </DataTrigger>
        <DataTrigger Binding="{Binding DaysSafe}" Value="5">
            <Setter Property="Foreground" Value="GreenYellow" />
        </DataTrigger>
    </Style.Triggers>
</Style>

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

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