简体   繁体   中英

How do I conditionally format a WPF TextBlock?

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.

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.

Thanks!

Adding some details to Daniel's (slightly short) answer as some of the needed DataTrigger stuff is not really trivial (like {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!

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

For example your 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM