【发布时间】: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