0

When I have a comboBox in my view and want a empty item to be able to unselect the option, I use this code in my view:

<ComboBox.Resources>
    <CollectionViewSource x:Key="comboBoxSource" Source="{Binding ElementName=ucPrincipal, Path=DataContext.MyProperty}" />
</ComboBox.Resources>
<ComboBox.ItemsSource>
    <CompositeCollection>
        <entities:MyType ID="-1"/>
        <CollectionContainer Collection="{Binding Source={StaticResource comboBoxSource}}" />
    </CompositeCollection>
</ComboBox.ItemsSource>

In this case, is the view which sets the ID to -1 to indicate that is special item. But I don't like so much this solution because the view model depends that the view sets it correctly.

So I am thinking to have this property in my view model:

public readonly MyType MyNullItem = new MyType();

But I don't know how to use it in my composite collection in the view instead of:

<entities:MyType ID="-1"/>

Is it possible?

Thanks.

4

1 回答 1

1

您需要某种绑定转换器,它将一个列表和一个对象组合成CompositeCollection. 前段时间我实现了类似的转换器,唯一的区别是将多个集合转换为一个:

/// <summary>
/// Combines multiple collections into one CompositeCollection. This can be useful when binding to multiple item sources is needed.
/// </summary>
internal class MultiItemSourcesConverter : IMultiValueConverter {
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
        var result = new CompositeCollection();

        foreach (var collection in values.OfType<IEnumerable<dynamic>>()) {
            result.Add(new CollectionContainer { Collection = collection });
        }
        return result;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) {
        throw new NotSupportedException();
    }
}

这种转换器的用法在 XAML 中如下所示:

<ComboBox.ItemsSource>
    <MultiBinding Converter="{StaticResource MultiItemSourcesConverter}">
        <Binding Path="FirstCollection" />
        <Binding Path="SecondCollection" />
    </MultiBinding>
</ComboBox.ItemsSource>
于 2016-05-24T16:00:40.930 回答