【问题标题】:How to Check Text Block Text value in Data Trigger in XAML如何在 XAML 中的数据触发器中检查文本块文本值
【发布时间】:2017-07-19 10:55:39
【问题描述】:

如果值为 xyz,我想检查文本块的文本值。我不想要任何操作,但如果文本值为“#FF84312F”,我想将此文本设置为文本的前景色。 下面是我的代码。 我怎样才能做到这一点。请帮帮我。

 <TextBlock Text="#FF84312F">
        <TextBlock.Style>
            <Style TargetType="TextBlock">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding  Path=Text, RelativeSource={RelativeSource Self}}" Value="*#">
                        <Setter Property="Foreground" Value="Red"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    试试这个:

    <TextBlock Text="#FF84312F">
        <TextBlock.Style>
            <Style TargetType="TextBlock">
                <Setter Property="Foreground" Value="{Binding Text,RelativeSource={RelativeSource Self}}" />
            </Style>
        </TextBlock.Style>
    </TextBlock>
    

    或者这个:

    <TextBlock Text="#FF84312F">
        <TextBlock.Style>
            <Style TargetType="TextBlock">
                <Style.Triggers>
                    <Trigger Property="Text" Value="#FF84312F">
                        <Setter Property="Foreground" Value="{Binding Text,RelativeSource={RelativeSource Self}}" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
    

    它将无条件或有条件地(使用Trigger)将Foreground 设置为Text 属性指定的值。

    【讨论】:

    • 谢谢先生,但文本值不是恒定的,可以更改。可能是颜色代码会有所不同。我想要诸如 contains() 类型的东西
    • 你在说什么?包含? XAML 中没有这样的东西。这与您最初的问题有什么关系?
    • 你有什么其他的解决方案可以帮到我吗?
    • 如果我提出另一个问题,那不适合我的解决方案。
    • 当然,如果您有其他问题,您应该再问一个问题。您可能还需要考虑在提问时详细解释您真正想要的内容......
    【解决方案2】:

    注意:

    这个答案是基于answer provided by mm8的cmets

    您可以使用转换器将您的字符串转换为SolidColorBrush

    转换器类:

    public class TextToSolidColorBrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var color = Brushes.Black;
            try
            {
                var converted = new BrushConverter().ConvertFromString(value?.ToString());
                color = converted != null ? (SolidColorBrush) converted : Brushes.Black;
            }
            catch (Exception e)
            {
                // ignored
            }
            return color;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    XAML:

    <Window.Resources>
        <local:TextToSolidColorBrushConverter x:Key="TextToSolidColorBrushConverter"/>
    </Window.Resources>
    
    <TextBlock Text="Any text">
        <TextBlock.Style>
            <Style TargetType="TextBlock">
                <Setter Property="Foreground" Value="{Binding Text,RelativeSource={RelativeSource Self}, Converter={StaticResource TextToSolidColorBrushConverter}}" />
            </Style>
         </TextBlock.Style>
    </TextBlock>
    

    【讨论】:

      【解决方案3】:

      根据您的评论

      我想要的是。我必须检查文本块文本,如果文本块文本 是颜色代码,然后将该颜色代码分配给前景。就是这样

      如果Binded 文本值是Color 代码,这将更改ColorTextblock Foreground,否则default colorshown

       <TextBlock Text="{Binding Text}" Foreground="{Binding Text, RelativeSource=
       {RelativeSource Self}}"/>
      

      <TextBlock Text="#0FFFFF" Foreground="{Binding Text, RelativeSource=
      {RelativeSource Self}}"/>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-15
        相关资源
        最近更新 更多