-->

Show if ItemsControl.ItemsSource is null

2020-07-16 08:56发布

问题:

Greetings,

I have a ItemsControl which template I changed to show a RadioButton for every object in the binded ItemsSource.

However the ItemsSource can be empty and when it is empty I'd like to show a default value. Something like "The binded list contains no items for you to select"...

One way I thought of is to set the ItemsControl.Visibility to Collapsed and have a TextBlock.Vsibility to Visible which shows the text.. But this would include a lot more data.

Is it possible to show a default value if the ItemsControl.ItemsSource is null?

回答1:

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



回答2:

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>


回答3:

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>


回答4:

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.