WPF列表框 - 空列表显示消息 [英] WPF Listbox - Empty List Display Message

查看:313
本文介绍了WPF列表框 - 空列表显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以建议最好的方式来显示文字广告(包含List Empty等文字),以便可见性被绑定到Items.Count。

Can anyone suggest the best way to display a Textblock (with a text such as "List Empty") so that it's visibility is bound to the Items.Count.

我已经尝试了以下代码,不能让它工作,所以认为我一定是做错了。

I have tried the following code and can't get it to work, so think that I must be doing it wrong.

    <ListBox x:Name="lstItems" 
        ItemsSource="{Binding ListItems}">
    </ListBox>
    <TextBlock Margin="4" FontStyle="Italic" FontSize="12" Text="List is empty" Visibility="Collapsed">
        <TextBlock.Style>
            <Style TargetType="{x:Type TextBlock}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=lstItems, Path=Items.Count}" Value="0">
                        <Setter Property="Visibility" Value="Visible" />
                    </DataTrigger>  
                </Style.Triggers>
            </Style>                            
        </TextBlock.Style>
    </TextBlock>


推荐答案

您的代码中的问题是设置文本块中的可见性本身具有比设置样式更高的优先级。因此,即使触发发生,触发器内的设置也不起作用。将XAML更改为:

The problem in your code is that setting the value of Visibility in the text block itself has higher priority than setting it in the style. So, even when the trigger occurs, the setting inside the trigger has no effect. Change the XAML to:

  <TextBlock Margin="4" FontStyle="Italic" FontSize="12" Text="List is empty" >
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
           <Setter Property="Visibility" Value="Collapsed" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=lstItems, Path=Items.Count}" Value="0">
                    <Setter Property="Visibility" Value="Visible" />
                </DataTrigger>  
            </Style.Triggers>
        </Style>                            
    </TextBlock.Style>
  </TextBlock>

Visibility的设置全都在风格中,它的作品(至少在我的演示项目中) 。

Where the setting of Visibility is all in the style and it works (at least in my demo project).

这篇关于WPF列表框 - 空列表显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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