【问题标题】:WPF converter for ObservableCollection combined with static items用于 ObservableCollection 的 WPF 转换器与静态项相结合
【发布时间】:2015-07-29 15:41:33
【问题描述】:

我正在尝试将ComboBox 绑定到ObservableCollection,这将有一些静态项目作为第一个项目,然后是集合中的实际项目。

No index
1% p.a.
5% p.a.
--------
Item1
Item2
Item3

我认为绑定转换器可以解决问题。

<ComboBox ItemsSource = "{Binding indexes}, Converter={StaticResource IndexCollectionConverter}}" />

在运行时添加和删除项目的集合。

public virtual ObservableCollection<indexation> indexes{ get; set; }

我需要总是有一些静态项目(前 3 个项目),然后是一个分隔符(可选,但如果可能的话很好),然后是实际 ObservableCollection 中的所有项目。

我知道 CompositeCollection 类,但在 XAML 中实现时,Visual Studio 输出窗口给我一个绑定错误。

所以我尝试了转换器,但我迷失了如何从转换方法返回这种集合。

public class IndexCollectionConverter: IValueConverter {

    public object Convert(object value, Type targetType, object parameter, Globalization.CultureInfo culture)
    {
        //LOST HERE...
    }

    public object ConvertBack(object value, Type targetType, object parameter, Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

应该如何定义转换器来返回集合?

【问题讨论】:

  • 为什么不在代码中填充静态项?创建索引 ObservableCollection,用静态项填充它,然后添加运行时加载的项。

标签: .net wpf observablecollection ivalueconverter


【解决方案1】:

试试 IMultiValueConverter:

public class IndexCollectionConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return values                               // loop through all values
            .Select(v => v as IEnumerable<object>)  // cast to something we can enumerate
            .Where(v => v != null)                  // ignore anything we can't
            .SelectMany(v => v)                     // concat them all into a single list
            .ToList();                              // turn into a memory array now
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

这是一些示例代码,我使用 XmlDataProviders 只是为了展示它的工作原理,但您可以将源绑定更改为任何内容:

<Window.Resources>

    <local:IndexCollectionConverter x:Key="IndexCollectionConverter" />

    <XmlDataProvider x:Key="StaticIndexes">
        <x:XData>
            <IndexData xmlns="">
                <Item Desc="No Index" />
                <Item Desc="1% p.a." />
                <Item Desc="2% p.a." />
            </IndexData>
        </x:XData>
    </XmlDataProvider>

    <XmlDataProvider x:Key="DynamicIndexes">
        <x:XData>
            <IndexData xmlns="">
                <Item Desc="Item1" />
                <Item Desc="Item2" />
                <Item Desc="Item3" />
            </IndexData>
        </x:XData>
    </XmlDataProvider>

</Window.Resources>

<ComboBox VerticalAlignment="Top" IsDropDownOpen="True">
    <ComboBox.ItemsSource>
        <MultiBinding Converter="{StaticResource IndexCollectionConverter}">
            <Binding Source="{StaticResource StaticIndexes}" XPath="IndexData/Item" />
            <Binding Source="{StaticResource DynamicIndexes}" XPath="IndexData/Item" />
        </MultiBinding>
    </ComboBox.ItemsSource>
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding XPath=@Desc}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    • 2011-11-17
    • 2020-12-21
    相关资源
    最近更新 更多