WPF 标签样式触发器基于标签内容中未知索引处的字符,是否可能?

发布于 2024-10-14 20:51:41 字数 609 浏览 1 评论 0原文

当表单上的字段要求发生变化时,是否可以使用触发器检查 Label.Contents 中的最后一个字符是否为“*”,如果是,则在标签上设置属性,而不是以编程方式设置样式?

类似这样,但如何检查 Content 属性的最后一个字符?

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海螺姑娘 2024-10-21 20:51:41

我认为你必须为此使用转换器。尝试使用类似

<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

的内容,只要您知道其位置,您就可以绑定到 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] (以我认为的任何方式)我知道)。

除此之外,我认为您必须按照您自己指出的那样执行代码隐藏解决方案

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文