gpt4 book ai didi

c# - 使用转换器的数据触发器不起作用

转载 作者:行者123 更新时间:2023-11-30 20:07:57 25 4
gpt4 key购买 nike

我正在尝试根据它的值更新文本 block 的颜色。看起来很简单但不起作用。

这是文本 block xaml。

 <TextBlock 
Grid.Column="1"
Grid.Row="1"
Text="{Binding Path=GL, StringFormat={}{0:N0}}"
HorizontalAlignment="Left"
FontFamily="Verdana"
Foreground="Tomato"
FontWeight="Bold"
VerticalAlignment="Center"
Margin="5,2,5,0"
FontSize="18"
>
<TextBlock.Resources>
<converters:ColorConverter x:Key="CoConverter"></converters:ColorConverter>
</TextBlock.Resources>
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text, Converter={StaticResource ResourceKey=CoConverter}}" Value="true">
<Setter Property="TextBlock.Foreground" Value="LimeGreen" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>

这里是转换器

public class ColorConverter : MarkupExtension, IValueConverter
{

#region IValueConverter Members

public object Convert(object value,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
if (value == null)
return false;
if (value.ToString().Length == 0)
return false;
if (System.Convert.ToDouble(value) >= 0)
return true;

return false;
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}

转换器看起来不错,但由于某种原因触发器不适用。

最佳答案

<TextBlock 
Grid.Column="1"
Grid.Row="1"
Text="{Binding Path=GL, StringFormat={}{0:N0}}"
HorizontalAlignment="Left"
FontFamily="Verdana"
FontWeight="Bold"
VerticalAlignment="Center"
Margin="5,2,5,0"
FontSize="18"
>
<TextBlock.Resources>
<converters:ColorConverter x:Key="CoConverter"></converters:ColorConverter>
</TextBlock.Resources>
<TextBlock.Style>
<Style>
<Setter Property="TextBlock.Foreground" Value="Tomato" />
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text, Converter={StaticResource ResourceKey=CoConverter}}" Value="true">
<Setter Property="TextBlock.Foreground" Value="LimeGreen" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>

您需要在您的样式中设置 Foreground 属性以在运行时动态更改它。

关于c# - 使用转换器的数据触发器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7724042/

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