WPF Label Style Trigger based on a char at unknown index in Contents of the Label, is it possible?

非 Y 不嫁゛ 提交于 2020-06-17 03:23:18

问题


Rather than programatically set the the style when the requirement of a field on a form changes is it possible to use a trigger that checks the last char in a Label.Contents is a '*' and if so set a property on the Label?

Something like this but how to check on the last char of the Content property?

  <Style x:Key="LabelStandard" TargetType="Label">
    <Setter Property="HorizontalAlignment"      Value="Left"/>
    <Setter Property="VerticalAlignment"        Value="Top"/>
    <Style.Triggers>
        <Trigger Property="Content" Value="*"> <!-TODO only check the last char -->
            <Setter Property="Foreground"      Value="Red"/>
        </Trigger>
    </Style.Triggers>
  </Style>

回答1:


I think you'll have to use a Converter for that. Try with something like this

<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();
    }
}

Update

You can bind to any given character in the Content string as long as you know its position.

<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>

But if your string length will vary (which I assume is the case) that won't do you much good since you can't bind the [2] inside the binding (in any way that I'm aware of).

Other than this, I think you'll have to do a code behind solution as you pointed out yourself



来源:https://stackoverflow.com/questions/4825686/wpf-label-style-trigger-based-on-a-char-at-unknown-index-in-contents-of-the-labe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!