使用转换器的数据触发不起作用
我正在尝试根据文本块的值更新其颜色。看似简单却行不通。
这是文本块 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;
}
}
转换器看起来不错,但是由于某种原因触发器没有应用。
I am trying to update the color of textblock depending on it value. Seems simple however not working.
Here's the textblock 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>
Here the converter
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;
}
}
The converter looks good, however the trigger is not applying for some reason.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要在样式中设置 Foreground 属性,以便在运行时动态更改它。
You need to set the Foreground property in your style to change it dynamically at runtime.
指定转换器时,您似乎在大括号内缺少 StaticResource:
Looks like you're missing StaticResource inside your curly braces when specifying the converter:
如果您尝试动态更改属性值,则相应的属性必须仅保留在 Setter 标记中。
但不能同时出现在 TextBlock 和 Setter 标记中。为了准确地说明您的示例,请删除我的代码中给出的 TextBlock 标记内的 Foreground 属性。
If you are trying to change property value dynamically, then corresponding property must be kept only in Setter tag.
But not in both TextBlock and Setter tag. To be precise for your example remove Foreground property inside the TextBlock tag as given in my code.