简体   繁体   中英

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.

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>

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}"/>

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