dotancohen
dotancohen

Reputation: 31531

How to make a TextBlock appear / disappear based on bound property of another TextBlock?

Supposing that I have two TextBlock elements, one being a label for the second, which is bound:

<TextBlock Margin="0,0,0,0" Text="Notes:" />
<TextBlock Margin="50,0,0,0" Text="{Binding Path=notes}" />

I only want these two TextBoxes to appear if notes!="", that is only if there is something to display. How would one go about this?

Thanks.

Upvotes: 0

Views: 957

Answers (2)

denis morozov
denis morozov

Reputation: 6316

so many ways to do it, DataTriggers, doing logic in your ViewModel, DependencyProp's in code behind so you can control everything through binding without any triggers, etc. or here's a sample doing in XAML only.

Copy/Paste/Run this code:

<Control>
        <Control.Style>
            <Style TargetType="Control">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Control">
                            <StackPanel x:Name="stackPanel">
                                <TextBlock Margin="0,0,0,0" Text="Notes:" />
                                <TextBlock x:Name="txtNotes" Margin="50,0,0,0" Text="{Binding Path=notes}" />
                            </StackPanel>
                            <ControlTemplate.Triggers>
                                <Trigger SourceName="txtNotes" Property="TextBlock.Text" Value="">
                                    <Setter TargetName="stackPanel" Property="Control.Visibility" Value="Collapsed"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Control.Style>
    </Control>

Upvotes: 2

Diego Mijelshon
Diego Mijelshon

Reputation: 52753

First create a converter:

public class EmptyStringToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,
                          CultureInfo culture)
    {
        return string.IsNullOrEmpty(value as string)
                   ? Visibility.Collapsed
                   : Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, 
                              CultureInfo culture)
    {
        throw new InvalidOperationException();
    }
}

Then reference it (you can do this in your App resources, in the view resources, etc:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Converters="clr-namespace:MyConverterNamespace">
  <Converters:EmptyStringToVisibilityConverter 
      x:Key="EmptyStringToVisibilityConverter"/>
</ResourceDictionary>

Then use it in your controls:

<TextBlock Margin="0,0,0,0" Text="Notes:"
           Visibility="{Binding notes,
               Converter={StaticResource EmptyStringToVisibilityConverter}"/>
<TextBlock Margin="50,0,0,0" Text="{Binding Path=notes}"
           Visibility="{Binding notes,
               Converter={StaticResource EmptyStringToVisibilityConverter}"/>

Upvotes: 1

Related Questions