gpt4 book ai didi

wpf - 如何使用触发器更改 TextBlock 的前景色?

转载 作者:行者123 更新时间:2023-12-03 23:16:28 25 4
gpt4 key购买 nike

我想从dependencyproperty 更改TextBlock 的前景色。
但我不改变文本块颜色。
我的代码中不知道这个问题。
如何使用触发器更改 TextBlock 的前景色?

XAML:

<TextBlock Name="TestBlock" Text="Test color" >
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property ="IsMouseOver" Value="True">
<Setter Property= "Foreground" Value="Gray"/>
</Trigger>
<DataTrigger Binding="{Binding Path=TestColorMode2, RelativeSource={RelativeSource AncestorType={x:Type local:Window1}}}" Value="0">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=TestColorMode2, RelativeSource={RelativeSource AncestorType={x:Type local:Window1}}}" Value="1">
<Setter Property="Foreground" Value="Blue" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=TestColorMode2, RelativeSource={RelativeSource AncestorType={x:Type local:Window1}}}" Value="2">
<Setter Property="Foreground" Value="Green" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=TestColorMode2, RelativeSource={RelativeSource AncestorType={x:Type local:Window1}}}" Value="3">
<Setter Property="Foreground" Value="Black" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>

代码:
public static DependencyProperty TestColorModeProperty = DependencyProperty.Register("TestColorMode", typeof(int), typeof(UpdateProgressItem));

public int TestColorMode
{
get { return (int)GetValue(TestColorModeProperty); }
set { SetValue(TestColorModeProperty, value); }
}

....

private void button1_Click(object sender, RoutedEventArgs e)
{
TestColorMode++;
}

最佳答案

也指定类型:

<Trigger Property="TextBlock.IsMouseOver" Value="true">
<Setter Property="TextBlock.Foreground" Value="Gray" />
</Trigger>

关于wpf - 如何使用触发器更改 TextBlock 的前景色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13914943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com