【问题标题】:How to change Button and Textblock style on binding如何在绑定时更改按钮和文本块样式
【发布时间】:2022-01-06 08:25:57
【问题描述】:

我正在尝试通过绑定更改按钮的样式以及按钮中包含的文本块的样式,但没有成功。 我想得到这样的东西:

我想根据绑定改变TextBlock前景色和Button BorderThickness。

这是我的代码:

<Button Name="WeldHistory"
Margin="20,0,20,0"
VerticalAlignment="Stretch"
Height="auto"
Background="{x:Null}"
BorderBrush="{StaticResource RedPolysoudeNormalBrush}"
BorderThickness="0 0 0 3"
Command="{Binding ScreenWeldHistoryCommand}" >
<Button.ContentTemplate>
    <DataTemplate>
        <TextBlock Text="{x:Static lang:UI_Text.SCREEN_PROGRAM_LIBRARY}" Typography.Capitals="AllSmallCaps" Typography.CapitalSpacing="True">
            <TextBlock.Style>
                <Style TargetType="{x:Type TextBlock}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsWeldHistoryActive}" Value="False">
                            <Setter Property="Foreground" Value="red"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
    </DataTemplate>
</Button.ContentTemplate>

【问题讨论】:

    标签: wpf xaml styles


    【解决方案1】:

    我认为问题应该出在您使用IsWeldHistoryActive 进行的绑定上。您可以尝试绑定到Button 的DataContext 吗?

    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding DataContext.IsWeldHistoryActive, 
                                       RelativeSource={RelativeSource AncestorType=Button, 
                                                       Mode=FindAncestor}}" 
                             Value="False">
                    <Setter Property="Foreground" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      相关资源
      最近更新 更多