过滤CollectionViewSource [英] Filtering CollectionViewSource
问题描述
我想通过过滤器将一个 ComboBox
绑定到我的数据。为此,我创建了一个 TextBox
和一个 ComboBox
。在后面的代码中,我读取一个文件,并生成存储为 ComboBox
项目的类Channel的对象。虽然编译器没有发出错误,但是过滤不能正常工作。如果我写的东西数据不见了,如果我擦除,它回来了。尝试和尝试后,我意识到,如果我开始输入myNamespace.myChannel(Unico.Canal),数据仍然保留,但不要过滤。奇怪的行为,的确。我怀疑我把东西放在错误的地方。
(为了更好地理解我已经翻译了代码,Canal = Channel) >
这是我的代码的方案:
命名空间Unico
{
public partial class ControlesArchivo:UserControl,INotifyPropertyChanged
{
public ControlesArchivo()
{
InitializeComponent();
}
public ObservableCollection< Channel> myListChannels //与INotifyPropertyChanged实现。但我认为我不需要它。
private void loadButton_Click(object sender,RoutedEventArgs e)
{
文件loadedFile = new File();
loadedFile.read(); //在列表中生成一堆数据。
foreach(Channel mychan in loadedFile.channels)//只是复制数据(也许这可以避免)
{
myListChannels.Add(mychan);
}
var view = CollectionViewSource.GetDefaultView(this.miListaDeCanales);
view.Filter = delegate(object o)
{
if(o.ToString()。包含(myTextBox.Text))//精致的地方
{
返回真
}
返回false;
};
myComboBox.ItemsSource = view;
DataContext = this;
}
private void myTextBox_TextChanged(object sender,TextChangedEventArgs e)
{
((ICollectionView)myComboBox。 ItemsSource).Refresh();
myComboBox.SelectedIndex = 0;
}
}
}
数据绑定在XAML中:
ItemsSource ={Binding view}
编辑:我想我知道问题在哪里:我没有指定要过滤的属性。我的意思是,您在ComboBox中看到的是myListChannels中列出的类Channel
的属性 channelName
。当我设置过滤器时,不应该知道我在过滤什么?我该怎么写这个?非常感谢。
是你的假设是正确的。
我假设你的翻译,
public ObservableCollection< Channel> myListChannels;
实际上是
code> public ObservableCollection< Canal> miListaDeCanales;
与命名空间中的 Canal
code> Unico
更新:
在过滤器中,尝试使用 ComboBox
中呈现的属性,而不是使用 ToString()
code> object ( o
)如果您没有覆盖 ToString()
从 System.Object
。
尝试切换
if(o.ToString()。包含(myTextBox.Text)
$ b $如果(((Canal)o).NameProperty.Contains(myTextBox.Text))
$ b
b
^^应该解决您的问题。
您在xaml中的 ComboBox.ItemTemplate
有一个 DataTemplate
。这将解释为什么你看到在 ComboBox
中呈现的有效值,否则所有的 ComboBoxItem
也将呈现为 Unico.Canal
I want to make a ComboBox
bound to my data, with a filter. For that I've created a TextBox
and a ComboBox
. In the code behind I read a file and generate objects of class Channel that are stored as items of the ComboBox
. Although the compiler throws no error the filtering doesn't work properly. If I write something the data is gone, if I erase, it's back. After trying and trying I've realized that if I started typing "myNamespace.myChannel" (Unico.Canal) the data remained, but don't filter. Strange behaviour, indeed. I suspect that I've put something in wrong place.
(for better understanding I've translated the code, Canal=Channel)
Here is the scheme of my code:
namespace Unico
{
public partial class ControlesArchivo : UserControl, INotifyPropertyChanged
{
public ControlesArchivo()
{
InitializeComponent();
}
public ObservableCollection<Channel> myListChannels //with INotifyPropertyChanged implemented. But I think I don't need it.
private void loadButton_Click(object sender, RoutedEventArgs e)
{
File loadedFile = new File();
loadedFile.read(); //Generates a bunch of data in lists.
foreach (Channel mychan in loadedFile.channels) //Just duplicating the data (maybe this can be avoided)
{
myListChannels.Add(mychan);
}
var view = CollectionViewSource.GetDefaultView(this.miListaDeCanales);
view.Filter = delegate(object o)
{
if (o.ToString().Contains(myTextBox.Text)) //Delicate place
{
return true;
}
return false;
};
myComboBox.ItemsSource = view;
DataContext = this;
}
private void myTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
((ICollectionView)myComboBox.ItemsSource).Refresh();
myComboBox.SelectedIndex = 0;
}
}
}
The data is bound in XAML with:
ItemsSource="{Binding view}"
EDIT: I think I know where is the problem: I'm not specifing the property to filter. I mean, what you see in the ComboBox is the property channelName
of the class Channel
listed in myListChannels. When I'm setting the filter, shouldn't I let know what I'm filtering? How could I write this? Thank you very much.
Yes your assumption is correct.
I'm assuming with your translations,
public ObservableCollection<Channel> myListChannels;
is actually
public ObservableCollection<Canal> miListaDeCanales;
with the class Canal
in the namespace Unico
Update:
In your filter try using the property that is rendered in the ComboBox
than use the ToString()
on the object
(o
) if you've not overridden ToString()
from System.Object
.
try switching
if (o.ToString().Contains(myTextBox.Text))
to
if (((Canal)o).NameProperty.Contains(myTextBox.Text))
^^ that should fix your issue.
Do you have a DataTemplate
for ComboBox.ItemTemplate
in xaml. That will explain why you see the valid value rendered in the ComboBox
, else all the ComboBoxItem
's will also render as Unico.Canal
这篇关于过滤CollectionViewSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!