我在Style中设置了前景:
<Style x:Key="ValueStyle" TargetType="{x:Type TextBlock}" BasedOn="{StaticResource SubHeadingFontStyle}">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
**<Setter Property="v:DynamicProperties.ForegroundKey" Value="{Binding Color, Mode=OneWay, Converter={v:SignalColorToResourceKeyConverter}}" />**
</Style>
我有DataTemplete:
<DataTemplate DataType="{x:Type vs:NumericVisualizerElement}">
<TextBlock x:Name="AverageValue"
**Style="{StaticResource ValueStyle}"**
Text="{Binding Path=AverageValue, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
<DataTemplate.Triggers>
**<DataTrigger Binding="{Binding Path=Color}" Value="None">
<Setter Property="TextBlock.Foreground" Value="Red" />
</DataTrigger>**
</DataTemplate.Triggers>
</DataTemplate>
在v:SignalColorToResourceKeyConverter中,如果Color为&#34; None&#34;然后返回DependencyProperty.UnsetValue。但是当颜色为无时,我想前景为红色。 当第一次加载是Color是None然后Foreground有红色,但是当我在运行时改变Color然后返回Color是None,Foreground不是Red,它是UnsetValue。怎么解决? 谢谢你的帮助!
编辑1: 我定义了枚举SignalColor
public enum SignalColor
{
None,
Signal01A,
Signal02A,
Signal03A,
}
在SignalColorToResourceKeyConverter.cs中和方法转换:
public object Convert(object value, Type tagertType, object parameter, System.Globalization.CultureInfo culture)
{
var signalColor = (SignalColor) value;
switch (signalColor)
{
case SignalColor.None:
return DependencyProperty.UnsetValue;
case SignalColor.Signal01A:
return "Signal01ABrush";
case SignalColor.Signal01B:
return "Signal01BBrush";
}
}
我希望当我选择颜色为None时,前景将覆盖为红色。我想在文件xaml中执行此操作。
答案 0 :(得分:0)
试试这个 Xaml代码:
Foreground="{Binding RowForeColor,
Converter={StaticResource StringToColorConverter}}"
转换器代码
public object Convert(object value, Type targetType, object parameter, string language)
{
if ((string)value == "Red")
{
return "Red";
}
else if((string)value == "White")
{
return "White";
}
else if ((string)value == "LightGray")
{
return "LightGray";
}
else if((string)value =="Pink")
{
return "Pink";
}
return "Black";
}