How to implement NullText in TextBlock with binding?

I would like to implement the "NullText" behavior for the TextBlock associated with the property in the ViewModel. When this property in the ViewModel is null or empty, I would like to display gray italic text, for example, β€œNo data”. I would like this to fit the MVVM pattern, but I'm lost ...

Update Therefore, after playing with the solution that James Webster proposed, I made it 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> 
+7
source share
3 answers

I would recommend using IValueConverter ; if the original value is not empty or empty, then pass it to a TextBlock. If the original value is null or empty, then visualize the selected 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 just realized that you also want to set the style ... hmmm, possibly a DataTrigger , which sets the style if I need the value "No data",

 <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 like these lines might work.

+4
source

I think you do not need to create a converter class, you can just 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 must specify the system namespace as

 xmlns:System="clr-namespace:System;assembly=mscorlib" 
+2
source

You can try to bind a property that looks like this

  private string _textBlockText; public string TextBlockText { get { if(string.IsNullOrEmpty(_textBlockText)) { return "No Data"; } return _textBlockText; } set { _textBlockText = value; } } 

and then use the XAML that James mentioned for styling. Saves the need for a converter.

0
source

All Articles