我认为您必须为此使用转换器。试试这样的东西
<Style x:Key="LabelStandard" TargetType="Label">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource self},
Path=Content,
Converter={StaticResource LastCharConverter},
ConverterParameter=*}"
Value="True">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
public class LastCharConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
{
return false;
}
string content = value.ToString();
if (content.Length > 0 &&
content[content.Length - 1] == (char)parameter)
{
return true;
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
更新
您可以绑定到Content 字符串中的任何给定字符,只要您知道它的位置。
<Style x:Key="LabelStandard" TargetType="Label">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource self},
Path=(Content)[2]}"
Value="*">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
但是,如果您的字符串长度会有所不同(我认为是这种情况),那对您没有多大好处,因为您无法在绑定中绑定 [2](以我知道的任何方式) .
除此之外,我认为您必须按照您自己指出的那样做一个解决方案背后的代码