显示 ItemsControl.ItemsSource 是否为空

本文关键字:是否 ItemsSource ItemsControl 显示 | 更新日期: 2023-09-27 17:56:19

问候,

有一个项目控制,我更改了该模板,为绑定的项源中的每个对象显示单选按钮。

但是,ItemsSource 可以为空,当它为空时,我想显示一个默认值。类似于"绑定列表不包含任何项目供您选择"...

我想到的一种方法是将ItemsControl.Visibility设置为折叠,并将TextBlock.Vsibility设置为Visible,以显示文本。但这将包括更多的数据。

如果 ItemsControl.ItemsSource 为空,是否可以显示默认值?

显示 ItemsControl.ItemsSource 是否为空

创建这个简单的转换器后:

public class AnyItemsToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var collection = value as IEnumerable;
        if (collection == null)
            return Visibility.Collapsed;
        return collection.OfType<object>().Any() ? Visibility.Collapsed : Visibility.Visible;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

您可以重写 ItemsControl 模板以使用相对源绑定来支持此功能。

<UserControl x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SilverlightApplication1">
    <UserControl.Resources>
        <local:AnyItemsToVisibilityConverter x:Key="AnyItemsToVisibilityConverter" />
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White">
        <ItemsControl>
            <ItemsControl.Template>
                <ControlTemplate TargetType="ItemsControl">
                    <Grid>
                        <TextBlock Text="No Items to Display" 
Visibility="{Binding Items, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource AnyItemsToVisibilityConverter}}" />
                        <ItemsPresenter />
                    </Grid>
                </ControlTemplate>     
            </ItemsControl.Template>
        </ItemsControl>
    </Grid>
</UserControl>

如果我理解正确,我认为您可以通过创建一个IValueConverter来解决您的问题。

您不应该创建一个转换器来显示您的列表是否为空。当您的XAML,转换器和数据源是完全独立的项目时,会更好。MVVM 不是关于松耦合的吗?

好吧,后面的代码是邪恶的。感谢您指出这一点。我更正了源代码,现在完全是声明式风格:

       <ControlTemplate x:Key="ListBoxTemplate" TargetType="ListBox">
            <StackPanel>
            <ItemsPresenter  
                 Visibility="{Binding Path=NotEmpty,
                Converter={StaticResource BoolToVisibilityConverter}}">
            </ItemsPresenter>
                <TextBlock Text="No items to select from" 
                 Visibility="{Binding Path=Empty,
                 Converter={StaticResource BoolToVisibilityConverter}}"/>
            </StackPanel>
        </ControlTemplate>
        <Style x:Key="ListBoxStyle2" TargetType="ListBox"  >
            <Setter Property="Template" Value="{StaticResource ListBoxTemplate}">
            </Setter>
            <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate>
                        <StackPanel />
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>
        </Style>
您可以

做的一件事是,在检查ItemsControl.ItemsSource为空后,您可以添加一个项目"The binded list contains no items for you to select"。我希望这会满足你的目的。