如何有条件地格式化WPF TextBlock?

时间:2022-08-22 21:13:08

I have a WPF TextBlock bound to a string. If that string is empty, I want the TextBlock to display a warning message in another colour.

我有一个绑定到字符串的WPF TextBlock。如果该字符串为空,我希望TextBlock以另一种颜色显示警告消息。

This is easy to do in code, I was wondering if there was a elegant WPF pure XAML solution for it? I have investigated Style Triggers, but the syntax doesn't come naturally to me.

这很容易在代码中做,我想知道是否有一个优雅的WPF纯XAML解决方案呢?我调查了样式触发器,但语法并不是我自然而然的。

Thanks!

谢谢!

2 个解决方案

#1


23  

Adding some details to Daniel's (slightly short) answer as some of the needed DataTrigger stuff is not really trivial (like {x:Null}):

将一些细节添加到Daniel的(略短)答案中,因为一些所需的DataTrigger东西并不是真正的微不足道(如{x:Null}):

<TextBlock Text="{Binding MyTextProperty}">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding MyTextProperty}" Value="{x:Null}">
                    <Setter Property="Text" Value="Hey, the text should not be empty!"/>
                    <Setter Property="Foreground" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

BTW: Did this completely from memory, did not check it in VS or Blend, so please excuse if there are errors in there. However, you should be able to sort them out yourself. What counts is the idea. Good luck!

顺便说一下:这完全是从记忆中得到的,没有在VS或Blend中检查过,所以请原谅那里是否有错误。但是,你应该能够自己解决它们。重要的是这个想法。祝你好运!

#2


9  

You can use Converter for this. Simply Create class with IValueConverter. After in dataBinding use this converter

你可以使用Converter。使用IValueConverter创建类。在dataBinding之后使用此转换器

For example your XAML

例如你的XAML

<Window x:Class="WpfApplication4.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:lib="clr-namespace:WpfApplication4"
    Title="Window1" Height="300" Width="300">

<Window.Resources>
    <lib:TextBlockDataConveter x:Key="DataConverter"/>
    <lib:TextBlockForegroundConverter x:Key="ColorConverter"/>
</Window.Resources>

<Grid>
    <TextBlock Text="{Binding Path=message, Converter ={StaticResource DataConverter}}" Foreground="{Binding message, Converter={StaticResource ColorConverter}}"/>
</Grid>

and your converters:

和你的转换器:

public class TextBlockDataConveter:IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
        {
            return "Error Message";
        }
        else
        {
            return value;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

class TextBlockForegroundConverter:IValueConverter
{

    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
        {

            SolidColorBrush brush = new SolidColorBrush();
            brush.Color = Colors.Red;
            return brush;
        }
        else
        {
            SolidColorBrush brush = new SolidColorBrush();
            brush.Color = Colors.Black;
            return brush;

        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

it works . Check it.

有用 。核实。

#1


23  

Adding some details to Daniel's (slightly short) answer as some of the needed DataTrigger stuff is not really trivial (like {x:Null}):

将一些细节添加到Daniel的(略短)答案中,因为一些所需的DataTrigger东西并不是真正的微不足道(如{x:Null}):

<TextBlock Text="{Binding MyTextProperty}">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding MyTextProperty}" Value="{x:Null}">
                    <Setter Property="Text" Value="Hey, the text should not be empty!"/>
                    <Setter Property="Foreground" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

BTW: Did this completely from memory, did not check it in VS or Blend, so please excuse if there are errors in there. However, you should be able to sort them out yourself. What counts is the idea. Good luck!

顺便说一下:这完全是从记忆中得到的,没有在VS或Blend中检查过,所以请原谅那里是否有错误。但是,你应该能够自己解决它们。重要的是这个想法。祝你好运!

#2


9  

You can use Converter for this. Simply Create class with IValueConverter. After in dataBinding use this converter

你可以使用Converter。使用IValueConverter创建类。在dataBinding之后使用此转换器

For example your XAML

例如你的XAML

<Window x:Class="WpfApplication4.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:lib="clr-namespace:WpfApplication4"
    Title="Window1" Height="300" Width="300">

<Window.Resources>
    <lib:TextBlockDataConveter x:Key="DataConverter"/>
    <lib:TextBlockForegroundConverter x:Key="ColorConverter"/>
</Window.Resources>

<Grid>
    <TextBlock Text="{Binding Path=message, Converter ={StaticResource DataConverter}}" Foreground="{Binding message, Converter={StaticResource ColorConverter}}"/>
</Grid>

and your converters:

和你的转换器:

public class TextBlockDataConveter:IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
        {
            return "Error Message";
        }
        else
        {
            return value;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

class TextBlockForegroundConverter:IValueConverter
{

    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
        {

            SolidColorBrush brush = new SolidColorBrush();
            brush.Color = Colors.Red;
            return brush;
        }
        else
        {
            SolidColorBrush brush = new SolidColorBrush();
            brush.Color = Colors.Black;
            return brush;

        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

it works . Check it.

有用 。核实。