从停止共享过滤器ItemsControls [英] Stopping ItemsControls from sharing filters

查看:130
本文介绍了从停止共享过滤器ItemsControls的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个ItemsControls,一是一个ListView,一个我开发一个自定义的控制。

I have two ItemsControls, one a ListView, and one a custom control I am developing.

我已两个控件的ItemsControl.ItemsSource属性设置为相同的IEnumerable对象,在这种情况下,列表

I have set the ItemsControl.ItemsSource property of both controls to the same IEnumerable object, in this case, a List.

我申请一个过滤器,以我的自定义控件的ItemsControl.Items财产(this.Items.Filter = myFilter),并预期我的控制刷新,仅显示符合筛选条件的项目。

I apply a filter to the ItemsControl.Items property of my custom control (this.Items.Filter = myFilter) and my control refreshes as expected, showing only the items that match the filter.

然而,使用相同的IEnumerable对象的ItemsControl.ItemsSource属性还刷新,仅显示符合我应用到我的自定义控件的过滤器,该项目的ListView。

However, the ListView using the same IEnumerable object for its ItemsControl.ItemsSource property also refreshes, showing only the items that match the filter I applied to my custom control.

谁能告诉我如何保持过滤器在我的自定义控制在我的列表视图影响的项目?

Can anyone tell me how to keep the filter in my custom control from affecting the items in my listview?

推荐答案

我能想到的第一件事,就是不需要任何大改动你所描述的是只包住的ItemsSource集合在你XAML在那里他们被分配一个CollectionViewSource。

The first thing I can think of, that doesn't require any bigger modifications to what you described is to just to wrap the ItemsSource collections in a CollectionViewSource in your XAML where they are being assigned.

<DockPanel>

	<Button DockPanel.Dock="Top"
			Content="Filter Lowercase Names"
			Click="OnFilterClick"/>

	<ListView x:Name="uiListView">
		<ListView.Resources>
			<CollectionViewSource x:Key="ItemsCollection"
								  Source="{Binding Names}" />
		</ListView.Resources>
		<ListView.ItemsSource>
			<Binding Source="{StaticResource ItemsCollection}" />
		</ListView.ItemsSource>
	</ListView>

	<ListBox x:Name="uiListBox"
			 ItemsSource="{Binding Names}" />

</DockPanel>

和再筛选逻辑:

public partial class Window1 : Window
{
	public Window1()
	{
		InitializeComponent();

		Names = new List<string>();

		Names.Add("Robert");
		Names.Add("Mike");
		Names.Add("steve");
		Names.Add("Jeff");
		Names.Add("bob");
		Names.Add("Dani");

		this.DataContext = this;
	}
	public List<String> Names { get; set; }

	private void OnFilterClick(object sender, RoutedEventArgs e)
	{
		uiListView.Items.Filter = x => x.ToString()[0] == x.ToString().ToUpper()[0];
	}
}

这篇关于从停止共享过滤器ItemsControls的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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