且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从停止共享过滤器ItemsControls

更新时间:2023-09-28 20:37:52

我能想到的第一件事,就是不需要任何大改动你所描述的是只包住的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];
	}
}