Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement NullText in a TextBlock with Binding?

Tags:

c#

wpf

I would like to implement a "NullText" behavior for a TextBlock that is bound to a property in a ViewModel. When that property in the ViewModel is null or empty, I would like to display gray italic text something like "No Data". I'd like this to follow MVVM pattern but I am lost...

Update So after playing around with the solution James Webster suggested, I got it to work like this...

<UserControl.Resources>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    <c:NullOrEmptyValueConverter x:Key="NullOrEmptyValueConverter" Text="(No Data)"/>
</UserControl.Resources>

 ...     

<TextBlock Name="SerialNumberTextBlock" Text="{Binding Path=SerialNumber, Converter={StaticResource NullOrEmptyValueConverter}}">
    <TextBlock.Resources>
        <Style TargetType="TextBlock">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=SerialNumberTextBlock, Path=Text}" Value="(No Data)">
                    <Setter Property="FontStyle" Value="Italic"/>
               </DataTrigger>
           </Style.Triggers>
        </Style>
    </TextBlock.Resources>
</TextBlock>
like image 654
Jim Avatar asked Apr 09 '11 01:04

Jim


Video Answer


2 Answers

I'd recommend implementing an IValueConverter; if the source value is not null or empty, then pass it through to the TextBlock. If the source value is null or empty, then render your chosen text.

public class NullValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string str = (string)value;
        if (str.IsNullOrWhitespace())
        {
            return "No Data";
        }
        return str;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        ... //An empty implementation I expect...
    }
}

However I have just realised that you want to set the style as well... hmmm, probably a DataTrigger that sets the style if the value is 'No Data' required I expect;

<TextBlock Text="{Binding Path=SomeProperty, Converter={StaticResource keyToNullValueConverter}">
    <TextBlock.Triggers>
        <DataTrigger Binding="{Binding Path=Text}" Value="No Data">
            <Setter Property="FontStyle" Value="Italic"/>
        </DataTrigger>
    </TextBlock.Triggers>
</TextBlock>

Something along those lines might work.

like image 51
James Webster Avatar answered Oct 04 '22 15:10

James Webster


I think you don't need to create Converter Class, you can simply write your style code like this.

<Style TargetType="TextBlock">             
<Style.Triggers>                 
<DataTrigger Binding="{Binding ElementName=SerialNumberTextBlock, Path=Text}" Value="{x:Null}">                     
<Setter Property="FontStyle" Value="Italic"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=SerialNumberTextBlock, Path=Text}" Value="{x:Static System:String.Empty}">                     
<Setter Property="FontStyle" Value="Italic"/>
</DataTrigger>
</Style.Triggers>         
</Style> 

Note :- You need to include the system namespace as

xmlns:System="clr-namespace:System;assembly=mscorlib" 
like image 36
pchajer Avatar answered Oct 04 '22 13:10

pchajer