【问题标题】:Remove row if textblock binded value is null如果 textblock 绑定值为 null,则删除行
【发布时间】:2016-09-03 23:38:02
【问题描述】:

我只是问了一个类似的问题,但我注意到我的不好,我需要隐藏整行而不仅仅是文本块。让我解释一下发生了什么。所以我有一个具有这种结构的 ListBox:

<ListBox VerticalAlignment="Stretch"
     ItemsSource="{Binding EventInfo}">
<ListBox.ItemTemplate>
    <DataTemplate>
        <Grid>
           <Grid.ColumnDefinitions>
               <ColumnDefinition Width="100"/>
               <ColumnDefinition Width="20"/>
           </Grid.ColumnDefinitions>
           <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
           </Grid.RowDefinitions>
          <TextBlock Text="Event:" FontWeight="Bold" Grid.Column="0" Grid.Row="0"/>
          <TextBlock Text="{Binding Name}" FontWeight="Bold" Grid.Column="1" Grid.Row="0"/>
          <TextBlock Text="Foo:" FontWeight="Bold" Grid.Column="0" Grid.Row="1"/>
          <TextBlock Text="{Binding Foo}" FontWeight="Bold" Grid.Column="1" Grid.Row="1"/>
         </Grid>
    </DataTemplate>
 </ListBox.ItemTemplate>
</ListBox>

所以我需要做的是隐藏所有包含 textblock/s 且值为 null 或空值的行,实际上我是这样在纯 xaml 中管理的:

<ListBox.Resources>
  <Style TargetType="TextBlock">
    <Style.Triggers>
      <Trigger Property="Text" Value="">
          <Setter Property="Visibility" Value="Collapsed" />
      </Trigger>
      <Trigger Property="Text" Value="{x:Null}">
          <Setter Property="Visibility" Value="Collapsed" />
      </Trigger>
    </Style.Triggers>
  </Style>
</ListBox.Resources>

但这不是一个好的解决方案。事实上,你怎么能看到我每个 textblock 都按行和列组织,所以我需要隐藏包含具有 null 值的文本块的行。

使用我的解决方案,我只隐藏具有 null 值的文本块,这是没用的,因为该值已经为空或 null。

有机会通过xaml来管理吗?我不知道如何在 xaml 中执行此操作,因为例如,如果我在中间隐藏一行,我将得到一个空白区域,该行包含非空值的文本块。我不知道情况是否清楚。

如果有不清楚的地方,请询问,我会尽力解释得更好。谢谢。

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    您可以相应地设置网格行高

    替换

    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    

    <Grid.RowDefinitions>
        <RowDefinition>
            <RowDefinition.Style>
                <Style TargetType="RowDefinition">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Name}" Value="">
                            <Setter  Property="Height" Value="0" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Name}" Value="{x:Null}">
                            <Setter  Property="Height" Value="0" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </RowDefinition.Style>
        </RowDefinition>
        <RowDefinition>
            <RowDefinition.Style>
                <Style TargetType="RowDefinition">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Foo}" Value="">
                            <Setter  Property="Height" Value="0" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Foo}" Value="{x:Null}">
                            <Setter  Property="Height" Value="0" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </RowDefinition.Style>
        </RowDefinition>
    </Grid.RowDefinitions>
    

    【讨论】:

    • Funk 我正在尝试实现您的代码,但我不明白:&lt;TextBlock Text="{Binding Name}" FontWeight="Bold" Grid.Column="1" Grid.Row="0"/&gt; 应该放在RowDefinitions 之后?
    • 好吧我现在明白了逻辑,很有趣,我不知道这种做法。真的感谢这个解决方案:)祝你有美好的一天。
    【解决方案2】:

    您尝试过转换器吗? 您可以编写一个将字符串转换为可见性的转换器,如下所示:

    public class EmptyNotVisibleConverter : MarkupExtension, 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 NotImplementedException();
        }
    
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (_converter == null)
                _converter = new EmptyNotVisibleConverter();
            return _converter;
        }
    
        private static EmptyNotVisibleConverter _converter = null;
    }
    

    并像这样在您的网格上使用它:

    <Grid Visibility={Binding Name, Converter={local:EmptyNotVisibleConverter}}>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-12
      • 1970-01-01
      • 1970-01-01
      • 2011-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-05
      相关资源
      最近更新 更多