如何在几个TextBlock控件上使用相同的样式,但每个控件都使用不同的绑定和触发器



我希望有几个TextBlock看起来大致相同,但每个都需要对另一个触发器和以另一种方式做出反应。我试着使用一种通用样式(MyTextBlockStyle),并在以后添加触发器。但我总是遇到错误,比如"属性‘style’已经声明了两次"或类似的情况。

为了解释我的意思,我用3个文本块做了一个例子。其中2个绑定到每个不同的CheckBox,并且每个都触发不同的属性(显示的文本与前景颜色)。第三个TextBlock应根据TextbBox的内容更改其背景颜色。我怎样才能取得这样的成就?

<UserControl.Resources>
        <Style x:Key="MyTextBlockStyle" TargetType="TextBlock">
            <Setter Property="Background" Value="Yellow"/>
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="Margin" Value="10"/>
            <Setter Property="Padding" Value="5"/>
        </Style>
</UserControl.Resources>
<Grid >
        <StackPanel Margin="10">
            <CheckBox x:Name="CheckBox01" Content="Change Background of TextBlock 1" IsChecked="False" Foreground="White" Margin="5" />
            <CheckBox x:Name="CheckBox02" Content="Change Background of TextBlock 2" IsChecked="False" Foreground="White" Margin="5" />
            <TextBox x:Name="TextBox03" Padding="10" Background="White" Text="Enter Text here ..." Tooltip="Change Background of TextBlock 3"/>
            <TextBlock Style="{StaticResource MyTextBlockStyle}" >
                <TextBlock.Style>
                    <Style TargetType="TextBlock">
                        <Setter Property="Text" Value="No" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=CheckBox01, Path=IsChecked}" Value="True">
                                <Setter Property="Text" Value="Yes!" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>
            <TextBlock Style="{Static Resource MyTextBlockStyle}" Text="Something different">
                <TextBlock.Style>
                    <Style TargetType="TextBlock">
                        <Setter Property="Foreground" Value="Red" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=CheckBox02, Path=IsChecked}" Value="True">
                                <Setter Property="Foreground" Value="Green" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>
            <TextBlock Style="{Static Resource MyTextBlockStyle}" Text="Anything else">
                <TextBlock.Style>
                    <Style TargetType="TextBlock">
                        <Setter Property="Background" Value="Yellow" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=TextBox03, Path=Text}" Value="">
                                <Setter Property="Background" Value="Blue" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>
        </StackPanel>
</Grid >

请尝试此代码。它有效。我想正如你所愿

  <Window.Resources>       
    <Style TargetType="TextBlock">              
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding ElementName=CheckBox01,Path=IsChecked}" Value="True"/>
                    <Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Tag}" Value="tb1"/>
                </MultiDataTrigger.Conditions>
                <Setter Property="TextBlock.Background" Value="Orange"/>
            </MultiDataTrigger>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding ElementName=CheckBox02,Path=IsChecked}" Value="True"/>
                    <Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Tag}" Value="tb2"/>
                </MultiDataTrigger.Conditions>
                <Setter Property="TextBlock.Background" Value="Pink"/>
            </MultiDataTrigger>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding ElementName=TextBox03,Path=Text}" Value=""/>
                    <Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Tag}" Value="tb3"/>
                </MultiDataTrigger.Conditions>
                <Setter Property="TextBlock.Background" Value="Green"/>
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <StackPanel Margin="10">           
        <CheckBox x:Name="CheckBox01" Content="Change Background of TextBlock 1" IsChecked="False"  Margin="5"/>          
        <CheckBox x:Name="CheckBox02" Content="Change Background of TextBlock 2" IsChecked="False" Margin="5"/>           
        <TextBox x:Name="TextBox03" Padding="10" Background="White" Text="Enter Text here ..."/>           
        <TextBlock Tag="tb1"/>
        <TextBlock Tag="tb2" Text="Something different"/>
        <TextBlock Tag="tb3" x:Name="tb3" Text="Anything else"/>               
    </StackPanel>
</Grid >

您正在查找BasedOn属性。它总是让我着迷。

<TextBlock>
            <TextBlock.Style>
                <Style TargetType="TextBlock" BasedOn="MyTextBlockStyle">
                    <Setter Property="Text" Value="No" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=CheckBox01, Path=IsChecked}" Value="True">
                            <Setter Property="Text" Value="Yes!" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>

相关内容

  • 没有找到相关文章