【问题标题】:Stopping ItemsControls from sharing filters阻止 ItemsControls 共享过滤器
【发布时间】:2009-06-30 22:41:25
【问题描述】:

我有两个 ItemsControl,一个是 ListView,一个是我正在开发的自定义控件。

我已将两个控件的 ItemsControl.ItemsSource 属性设置为同一个 IEnumerable 对象,在本例中为 List。

我对自定义控件 (this.Items.Filter = myFilter) 的 ItemsControl.Items 属性应用过滤器,并且我的控件按预期刷新,仅显示与过滤器匹配的项目。

但是,使用相同 IEnumerable 对象作为其 ItemsControl.ItemsSource 属性的 ListView 也会刷新,仅显示与我应用于自定义控件的过滤器匹配的项目。

谁能告诉我如何让我的自定义控件中的过滤器不影响我的列表视图中的项目?

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    我能想到的第一件事,不需要对您所描述的内容进行任何更大的修改,只需将 ItemsSource 集合包装在您的 XAML 中的 CollectionViewSource 中,并在其中分配它们。

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

    【讨论】:

      【解决方案2】:

      这个组件

      http://dotnetexplorer.blog.com/2011/04/07/wpf-itemscontrol-generic-staticreal-time-filter-custom-control-presentation/

      能够过滤两个 WPF 项目控件,两者都绑定到同一个数据源而没有任何干扰!

      而且他是完整的 XAML 声明,没有 C# 代码!

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 2011-12-20
        • 2021-01-27
        • 2015-06-13
        • 2017-11-25
        • 1970-01-01
        • 1970-01-01
        • 2020-04-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多