XAML Conditional Binding in DataTemplate

I have a DataTemplate for a ListView in XAML:

    <DataTemplate x:Key="ResultItemTemplate">
        <Grid Grid.Column="2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="auto" />
                    </Grid.RowDefinitions>
                    <TextBlock Text="{Binding Name}" 
                               Margin="0,10,20,0" 
                               Grid.Column="0"
                               Grid.Row="0"/>

                    <TextBlock Text="{Binding TimeStamp}" 
                               Margin="0,10,10,0" 
                               Grid.Column="1"
                               Grid.Row="0"/>

                    <TextBlock Text="{Binding Text}" 
                               Grid.Column="0"
                               Grid.ColumnSpan="2"
                               Grid.Row="1"
                               TextWrapping="Wrap" 
                               Height="auto"
                               Margin="0,0,10,10"/>

                    <TextBlock Text="{Binding Additional}" 
                               Grid.Column="0"
                               Grid.ColumnSpan="2"
                               Grid.Row="2"
                               TextWrapping="Wrap" 
                               Height="auto"
                               Margin="0,0,10,20" />
                </Grid>
        </DataTemplate>

So applying this DataTemplate to my ListView, the Additional TextBlock is not present in every list item.

However the spacing exists for the Additional TextBlock whether the DataBinding value is null or not.
How do I get the text block to only be added when the 'Additional' Binding property exists?


This is your Converter:

namespace ValueConveters
{
    public class NullToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value == null ? Visibility.Collapsed : Visibility.Visible;
        }

        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new System.NotImplementedException();
        }
    }
}

And this is your XAML: put this in your root element:

xmlns:conveters="clr-namespace:ValueConveters"

and then this in your Resources:

<conveters:NullToVisibilityConverter x:Key="NullToVisibilityConverter"/>
<DataTemplate x:Key="ResultItemTemplate">
    <Grid Grid.Column="2">
       <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="auto" />
       </Grid.ColumnDefinitions>
       <Grid.RowDefinitions>
          <RowDefinition Height="auto" />
          <RowDefinition Height="*" />
          <RowDefinition Height="auto" />
       </Grid.RowDefinitions>
       <TextBlock Text="{Binding Name}" 
           Margin="0,10,20,0" 
           Grid.Column="0"
           Grid.Row="0"/>
       <TextBlock Text="{Binding TimeStamp}" 
           Margin="0,10,10,0" 
           Grid.Column="1"
           Grid.Row="0"/>
       <TextBlock Text="{Binding Text}" 
           Grid.Column="0"
           Grid.ColumnSpan="2"
           Grid.Row="1"
           TextWrapping="Wrap" 
           Height="auto"
           Margin="0,0,10,10"/>
       <TextBlock Text="{Binding Additional}" 
           Visibility="{Binding Additional,Converter={StaticResource NullToVisibilityConverter}}"
           Grid.Column="0"
           Grid.ColumnSpan="2"
           Grid.Row="2"
           TextWrapping="Wrap" 
           Height="auto"
           Margin="0,0,10,20" />
    </Grid>
</DataTemplate>

To collapse the TextBlock when Additional property is not available, try to bind TextBlock 's Visiblity property with TargetNullValue set to Collapsed . I would suggest something like this :

<TextBlock Text="{Binding Additional}" 
           Grid.Column="0"
           Grid.ColumnSpan="2"
           Grid.Row="2"
           TextWrapping="Wrap" 
           Height="auto"
           Margin="0,0,10,20"
           Visibility="{Binding Additional, TargetNullValue=Collapsed}"
           />

But turned out it doesn't work for me, for a reason I don't know. But binding this way did the trick :

<TextBlock DataContext="{Binding Additional}"
           Text="{Binding}" 
           Grid.Column="0"
           Grid.ColumnSpan="2"
           Grid.Row="2"
           TextWrapping="Wrap" 
           Height="auto"
           Margin="0,0,10,20"
           Visibility="{Binding TargetNullValue=Collapsed}"
           />

Related questions :

  • How to Set TargetNullValue to Visibility.Collapsed in Binding
  • Control not being hidden when binding source is null
  • Binding Visibility to DataContext

  • Based on Flat Eric's suggestion - fields can be bound to the IsVisible property:

    <Label IsVisible="{Binding Foo}" Text="{Binding Foo}"/>
    

    When the value is null the Label is hidden.

    链接地址: http://www.djcxy.com/p/44644.html

    上一篇: WPF DataTemplate文本块绑定

    下一篇: DataTemplate中的XAML条件绑定