DataTemplate中的XAML条件绑定

我在XAML中有一个ListView的DataTemplate:

    <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>

因此,将此DataTemplate应用于我的ListView,附加TextBlock不会出现在每个列表项中。

但是,无论DataBinding值是否为空,其他TextBlock的间隔都会存在。
如何让文本块仅在“附加”绑定属性存在时添加?


这是你的转换器:

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();
        }
    }
}

这是你的XAML:把它放在你的根元素中:

xmlns:conveters="clr-namespace:ValueConveters"

然后在你的资源中:

<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>

要在Additional属性不可用时折叠TextBlock ,请尝试将TextBlockVisiblity属性与TargetNullValue设置为Collapsed 。 我会建议这样的事情:

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

但事实证明,这并不适用于我,因为我不知道。 但以这种方式进行绑定的确有窍门

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

相关问题:

  • 如何将TargetNullValue设置为可见性。在绑定中混合
  • 当绑定源为空时,控件不会被隐藏
  • 绑定可见性到DataContext

  • 基于Flat Eric的建议 - 字段可以绑定到IsVisible属性:

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

    当值为空时,标签被隐藏。

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

    上一篇: XAML Conditional Binding in DataTemplate

    下一篇: XAML: Custom Binding in DataTemplate for use in GridViewColumn CellTemplate