【问题标题】:WPF Label Style Trigger based on a char at unknown index in Contents of the Label, is it possible?WPF 标签样式触发器基于标签内容中未知索引处的字符,这可能吗?
【发布时间】:2011-01-28 06:58:04
【问题描述】:

当表单上的字段要求发生变化时,与其以编程方式设置样式,不如使用触发器来检查 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>

【问题讨论】:

    标签: wpf xaml styles


    【解决方案1】:

    我认为您必须为此使用转换器。试试这样的东西

    <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](以我知道的任何方式) .

    除此之外,我认为您必须按照您自己指出的那样做一个解决方案背后的代码

    【讨论】:

    • 嗯.. 这么多工作.. 我宁愿以编程方式完成它 :) lbl.Style = (Style)this.Resources["LabelRequired"];
    • 感谢您的更新 - 它是如此接近的人想尝试让它工作..但似乎没有办法..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    相关资源
    最近更新 更多