XAML - Generic textbox stylewith triggers / parameters?

后端 未结 2 1217
野趣味
野趣味 2021-01-16 11:51

I want to be able to have a generic style template that can switch colors of my textboxes based on a bool. But I don\'t want to have to create a unique style for each textb

相关标签:
2条回答
  • 2021-01-16 12:17

    You can use the Tag property of TextBox

    <TextBlock Text="{Binding Text1}" Style={DynamicResource MyTextBoxTemplate} Tag="{Binding MyBool1}" />
    <TextBlock Text="{Binding Text2}" Style={DynamicResource MyTextBoxTemplate} Tag="{Binding MyBool2}" />
    <TextBlock Text="{Binding Text3}" Style={DynamicResource MyTextBoxTemplate} Tag="{Binding MyBool3}" />
    
    <Style x:Key="MyTextBoxTemplate" TargetType="TextBlock">
        <Style.Triggers>
            <Trigger Property="Tag" Value="True">
                <Setter Property="Foreground" Value="Green" />
            </Trigger>
            <Trigger Property="Tag" Value="False">
                <Setter Property="Foreground" Value="Red" />
            </Trigger>
        </Style.Triggers>
    </Style>
    
    0 讨论(0)
  • 2021-01-16 12:35

    You can also use an attached property for that instead of Tag.

    0 讨论(0)
提交回复
热议问题