Show if ItemsControl.ItemsSource is null

走远了吗. 提交于 2019-12-04 08:32:31

If I understood correctly, I think you can solve your problem by creating an IValueConverter.

After creating this simple converter:

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();
    }
}

You can override the ItemsControl Template to suppor this using RelativeSource Binding.

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

You should not create a Converter which shows wether your List is empty or not. It is better when your XAML,Converter and data source are totally independent items. Isn't MVVM about loose coupling?

OK, code behind is evil. Thanks for pointing that out. I corrected the source code, it is totally declarative style now:

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

One thing you could do is that after checking ItemsControl.ItemsSource is null, you could add a single item "The binded list contains no items for you to select". I hope this would server your purpose.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!