如果文本块绑定值为空,则删除行 [英] Remove row if textblock binded value is null

查看:26
本文介绍了如果文本块绑定值为空,则删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I just ask a similar question but I noticed for my bad, I need to hide the entire row and not only the textblock. Let me explain what happen. So I've a ListBox with this structure:

<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 的所有行,实际上我在纯 xaml 中像这样管理它:

so what I need to do is hide all the rows that contains the textblock/s with a null or empty value, actually I managed this in pure xaml like this:

<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,所以我需要隐藏包含具有空值的文本块的行.

but this is not a good solution. Infact, how you can see I've each textblock organized by row and column, so I need to hide the row that contain the textblock with the null value.

在我的解决方案中,我只隐藏具有空值的文本块,这是无用的,因为该值已经为空或为空.

With my solution, I only hide the textblock with null value, that is useless 'cause the value is already empty or null.

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

There is a chance to manage it through xaml? I've no idea how can I do this in xaml, 'cause if I hide a row in the middle for example, I'll get a blank space with the row that contains textblocks with not null value. I don't know if the situation is clear.

如果有什么不清楚的,请提问,我会尽量解释得更好.谢谢.

If something is unclear, ask and I'll try to explain better. Thanks.

推荐答案

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

You could set the Grid Row Height accordingly

替换

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

这篇关于如果文本块绑定值为空,则删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆