WPF绑定ObservableCollection与转换器 [英] WPF binding ObservableCollection with converter

查看:243
本文介绍了WPF绑定ObservableCollection与转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ObservableCollection字符串,我正在将其绑定到转换器到ListBox,并只显示以一些前缀开头的字符串。

我写道:

  public ObservableCollection< string>名称{get;组; } 

public MainWindow()
{
InitializeComponent();
Names = new ObservableCollection< Names>();
DataContext = this;
}

转换器:



转换(对象值,类型targetType,对象参数,System.Globalization.CultureInfo文化)
{pre $
if(value == null)
返回null;
return(value as ICollection< string>)。其中((x)=> x.StartsWith(A));
}

public object ConvertBack(object value,Type targetType,object parameter,System.Globalization.CultureInfo culture)
{
return null;
}
}

和XAML:

 < ListBox x:Name =filesListItemsSource ={Binding Path = Names,Converter = {StaticResource NamesListConverter}}/> 

但是列表框在其更新(添加或删除)后不会更新。

我注意到,如果我删除转换器的绑定,它的工作完美。
我的代码有什么问题?

解决方案

您的转换器正在从原始ObservableCollection中的对象创建新的集合。使用绑定设置的ItemsSource不再是原来的ObservableCollection。
为了更好地理解,这等于你写的内容:

  public object Convert(object value,Type targetType ,对象参数System.Globalization.CultureInfo文化)
{
if(value == null)
return null;
var list =(value as ICollection< string>)。其中((x)=> x.StartsWith(A))ToList();
返回列表;
}

转换器返回的列表是来自源集合的数据副本的新对象。原始集合中的进一步更改不会反映在新列表中,因此ListBox不了解该更改。
如果要过滤数据,请查看 CollectionViewSource



编辑:如何过滤

  public ObservableCollection< string> ;名称{get;组; } 
public ICollectionView View {get;组; }
public MainWindow()
{
InitializeComponent();

Names = new ObservableCollection< string>();
var viewSource = new CollectionViewSource();
viewSource.Source = Names;

//获取ICollectionView并设置Filter
View = viewSource.View;

//过滤谓词,只有以A开头的项目
View.Filter = o => o.ToString()。StartsWith(A);

DataContext = this;
}

在XAML中将ItemsSource设置为CollectionView

 < ListBox x:Name =filesListItemsSource ={Binding Path = View}/> 


I have an ObservableCollection of strings and I'm tring to bind it with converter to ListBox and show only the strings that start with some prefix.
I wrote:

public ObservableCollection<string> Names { get; set; }

public MainWindow()
{
    InitializeComponent();
    Names= new ObservableCollection<Names>();
    DataContext = this;
}

and the converter:

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

and the XAML:

<ListBox x:Name="filesList" ItemsSource="{Binding Path=Names, Converter={StaticResource NamesListConverter}}" />

but the listbox do not update after its beed update (add or remove).
I have notice that if I removes the converter from the binding its works perfectly. What is wrong with my code?

解决方案

Your converter is creating new collection from objects in the original ObservableCollection. The ItemsSource that is set using your binding is no longer the original ObservableCollection. To understand better, this is equal to what you have wrote:

 public object Convert(object value, Type targetType, object parameter,  System.Globalization.CultureInfo culture)
  {
      if (value == null)
         return null;
      var list = (value as ICollection<string>).Where((x) => x.StartsWith("A")).ToList();
      return list;
   }

The list that converter is returning is new object with copy of data from source collection. Further changes in original collection are not reflected in that new list so the ListBox does not know about that changes. If you want to filter your data take a look into CollectionViewSource.

EDIT: How to filter

     public ObservableCollection<string> Names { get; set; }
     public ICollectionView View { get; set; }
     public MainWindow()
     {
       InitializeComponent();

       Names= new ObservableCollection<string>();
       var viewSource  = new CollectionViewSource();
       viewSource.Source=Names;

      //Get the ICollectionView and set Filter
       View = viewSource.View;

      //Filter predicat, only items that start with "A"
       View.Filter = o => o.ToString().StartsWith("A");

       DataContext=this;
    }

In the XAML set the ItemsSource to the CollectionView

<ListBox x:Name="filesList" ItemsSource="{Binding Path=View}"/>

这篇关于WPF绑定ObservableCollection与转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆