5

问候,

我有一个 ItemsControl,我更改了哪个模板来为绑定的 ItemsSource 中的每个对象显示一个 RadioButton。

但是 ItemsSource 可以为空,当它为空时,我想显示一个默认值。诸如“绑定列表不包含可供您选择的项目”之类的内容...

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

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

4

4 回答 4

4

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

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

您可以使用RelativeSource Binding 覆盖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>
于 2011-05-12T14:27:41.657 回答
1

如果我理解正确,我认为您可以通过创建一个IValueConverter.

于 2011-05-12T11:39:29.360 回答
1

您不应该创建一个转换器来显示您的列表是否为空。当您的 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>
于 2011-05-13T22:32:59.357 回答
0

您可以做的一件事是,在检查ItemsControl.ItemsSource为 null 之后,您可以添加一个 item "The binded list contains no items for you to select"。我希望这能达到你的目的。

于 2011-05-12T11:35:06.913 回答