C# WPF (XAML) checking for empty string in DataTrigger without additional EmptyString converter

When working with WPF and XAML you may need to check whether certain binding field have a default value or not.
Let suppose we have a simple xaml view file Foo.xaml with data context  FooViewModel.cs – which in this case is the view model which we are using for data binding.

Our FooViewModel contains 2 properties (in this case string type) , which we want to bind our view:

private string _comment;
public string Comment
{
    get=>  _comment;
    set
    {
        _comment = value;
        RaisePropertyChangedNotification();
    }
}
 
private string _additionalComment;
public string AdditionalComment
{
    get=>  _additionalComment;
    set
    {
        _additionalComment= value;
        RaisePropertyChangedNotification();
    }
}

In out Foo.xaml view, we have 2 text boxes: Comment and Additional comment. The idea is to hide or show the additional comment field depending on the Comment field value. If the Comment field is empty, we should hide Additional comment.
On of the ways to achieve this is to write a XAML converter which checks comment field value and return Visibility depending on the string value. Something like a: 

//EmptyStringToVisibilityConverter.cs
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    string str = (string)value;//our comment value
    return string.IsNullOrEmpty(str) ? Visibility.Hidden : Visibility.Visible; 
    
}
and so on...

But we can skip this scenario and avoid creating additional converter using native xaml syntax. In order to do that we should override TextBox style and add DataTrigger which is bound to AdditionalComment field. So when Comment field is empty AdditionalComment  TextBox will be hidden. It looks like this:

<TextBox Text="{Binding AdditionalComment}">
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Setter Property="Visibility" Value="Visible" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Comment, FallbackValue=0, TargetNullValue=0}" Value="0">
                    <Setter Property="Visibility" Value="Hidden" />
               </DataTrigger>
           </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

This approach can be used for any nullable type.  
FallbackValue
This is used when a binding cannot determine a value at all, based on the the data source and the path. Or in other words, FallbackValue is used when the property bound to the source is not at all available. In that case, the value supplied for FallbackValue will be considered at the target end.
TargetNullValue
As the name suggests, when a source object’s property is null, the TargetNullValue is the alternate value you want to set for the target. So, whatever value is set for TargetNullValue will be set for the target.

 

That’s it 🙂

 

Tell us about your thoughtsWrite message

Your email address will not be published. Required fields are marked *