我有一个ObservableCollection的字符串,我打算将它与转换器绑定到ListBox并只显示以某些前缀开头的字符串。
我写道:WPF绑定ObservableCollection与转换器
public ObservableCollection<string> Names { get; set; }
public MainWindow()
{
InitializeComponent();
Names= new ObservableCollection<Names>();
DataContext = this;
}
和转换器:
class NamesListConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
return null;
return (value as ICollection<string>).Where((x) => x.StartsWith("A"));
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
和XAML:
<ListBox x:Name="filesList" ItemsSource="{Binding Path=Names, Converter={StaticResource NamesListConverter}}" />
但列表框中的木珠更新(添加或删除)后不更新。
我已经注意到,如果我从绑定中删除转换器的作品完美。 我的代码有什么问题?
您无法使用DynamicResource转换器。抛出异常 – user2348001 2013-05-04 09:46:34