开发者

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

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"/>
    <Styl开发者_如何学Ce.Triggers>
        <Trigger Property="Content" Value="*"> <!-TODO only check the last char -->
            <Setter Property="Foreground"      Value="Red"/>
        </Trigger>
    </Style.Triggers>
  </Style>


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜