Xamly确定ListBox.Items.Count> 0 [英] Xamly determine if a ListBox.Items.Count > 0

查看:65
本文介绍了Xamly确定ListBox.Items.Count> 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在XAML中是否可以确定列表框是否有数据?

Is there a way in XAML to determine if the ListBox has data?

如果没有数据,我想将其IsVisibile属性设置为false.

I wanna set its IsVisibile property to false if no data.

推荐答案

ListBox包含一个

The ListBox contains a HasItems property you can bind to. So you can just do this:

<BooleanToVisibilityConverter x:Key="BooleanToVisibility" />
...
<ListBox 
    Visibility="{Binding HasItems, 
      RelativeSource={RelativeSource Self}, 
      Converter=BooleanToVisibility}" />

或者作为触发器,因此您不需要转换器:

Or as a Trigger so you don't need the converter:

<ListBox>
  <ListBox.Style>
    <Style TargetType="{x:Type ListBox}">
      <Setter Property="Visibility" Value="Visible" />
      <Style.Triggers>
        <DataTrigger 
            Binding="{Binding HasItems, RelativeSource={RelativeSource Self}}"
            Value="False">
          <Setter Property="Visibility" Value="Hidden" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </ListBox.Style>
</ListBox>

我还没有测试绑定,所以可能会有一些错别字,但是您应该了解一下.

I haven't tested the bindings so there might be some typos but you should get the idea.

这篇关于Xamly确定ListBox.Items.Count&gt; 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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