可以使用以下代码实现:

<Window.Resources>
    <Style TargetType="{x:Type TextBlock}" x:Key="ColorStyle">
        <Style.Triggers>
            <DataTrigger Binding="{Binding MyInt}" Value="1">
                <Setter Property="Foreground" Value="Red"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding MyInt}" Value="2">
                <Setter Property="Foreground" Value="Green"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding MyInt}" Value="3">
                <Setter Property="Foreground" Value="Blue"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<TextBlock Text="{Binding MyInt}" Style="{StaticResource ColorStyle}"/>

其中,MyInt为绑定的int类型变量。当MyInt为1时,TextBlock的前景色为红色;当MyInt为2时,TextBlock的前景色为绿色;当MyInt为3时,TextBlock的前景色为蓝色。

wpf使用int变量使用DataTrigger绑定三种颜色

原文地址: http://www.cveoy.top/t/topic/LZ3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录