【问题标题】:ICollectionView Filter won't refresh - are my bindings wrong?ICollectionView 过滤器不会刷新 - 我的绑定错误吗?
【发布时间】:2012-01-10 07:07:07
【问题描述】:

我正在编写一个程序来查看图像,按文件名列出它们,并有一个用于搜索的文本框,它会在您输入时自动过滤列表。

我可以看到过滤器实际工作 - 看起来好像刷新也在触发 - 但是它没有在屏幕上“刷新”。

代码

public MainWindow()
{
    InitializeComponent();
    DataContext = this;
    ICollectionView collectionView = CollectionViewSource.GetDefaultView(AllImages);
    new DynamicFiltering(collectionView, this.TextBoxFilter);
}

public class DynamicFiltering
{
    public DynamicFiltering(ICollectionView filteredView, TextBox textBox)
    {
        string filterText = "";
        filteredView.Filter = delegate(object item)
        {
            MyImages textvalue = item as MyImages;
            string textvaluestring = textvalue.Name as string;
            if (textvaluestring.ToUpper().Contains(filterText.ToUpper()))
                return true;
            else
                return false;
        };
        textBox.TextChanged += delegate
        {
            filterText = textBox.Text;
            filteredView.Refresh();
        };
    }
}

xaml 是

<TextBox x:Name="TextBoxFilter"/>

<ListBox Name="VisualList"
         Template="{StaticResource StandardListBox}"
         DataContext="{Binding AllImages}"
         ItemsSource="{Binding}"
         Width="Auto"
         Grid.Row="1"/>

是列表框绑定的问题吗?另一件可能值得注意的事情是我在列表中的每个对象中都保存了一个字符串和图像源。


编辑: 下面是我用来填充 AllImages 的代码 - 使用 Environment.SpecialFolder.MyPictures 作为示例。

    public class MyImages
    {
        public ImageSource _image;
        public string _name;

        public MyImages(ImageSource image, string name)
        {
            _image = image;
            _name = name;
        }

        public override string ToString()
        {
            return _name;
        }

        public ImageSource Image
        {
            get { return _image; }
        }

        public string Name
        {
            get { return _name; }
        }

    }

    public List<MyImages> AllImages
    {
        get
        {
            List<MyImages> result = new List<MyImages>();
            foreach (string filename in
                System.IO.Directory.GetFiles(
                Environment.GetFolderPath(
                Environment.SpecialFolder.MyPictures)))
            {
                try
                {
                    result.Add(
                    new MyImages(
                    new BitmapImage(
                    new Uri(filename)),
                    System.IO.Path.GetFileNameWithoutExtension(filename)));
                }
                catch { }
            }
            return result;
        }
    }

【问题讨论】:

  • 您能否包含AllImages 的定义?它是什么类型的?另外,我假设当您启动应用程序时,会显示完整的图像列表,但从未过滤过?
  • @EsotericScreenName 我已更新原始帖子以包含列表 AllImages。是的,这是正确的 - 无论我使用静态过滤器还是尝试使用更新文本框内容的过滤器来稍微幻想一下。然而,在代码中,过滤器似乎确实在运行。

标签: c# wpf xaml binding filter


【解决方案1】:

你的直觉走上了正轨,问题出在你的绑定上。您的 ItemsSource 未绑定到您的过滤器正在使用的同一视图实例。试试这个方法:

    public ICollectionView CollectionView { get; set; }
    public MainWindow()
    {
        InitializeComponent(); 
        DataContext = this;
        CollectionView = CollectionViewSource.GetDefaultView(AllImages);
        new DynamicFiltering(CollectionView, this.TextBoxFilter);
    }

    <ListBox Name="VisualList" 
         DataContext="{Binding CollectionView}" 
         ItemsSource="{Binding}" 
         Width="Auto" 
         Grid.Row="1"/>

【讨论】:

  • 看准队友,非常感谢你。我最初尝试将其绑定到“collectionView”实例,但似乎我将原始设置略有错误。现在工作得很好。谢谢!
【解决方案2】:

不用调用 Refresh,只需重新设置过滤器即可。

【讨论】:

  • 配合我对另一个答案的回答,我认为问题不在于刷新,而在于错误的绑定。但是我无法确定任何其他可能的 ItemSource。
【解决方案3】:

你的感觉是对的,问题出在 Listbox 的绑定上。去掉DataContext设置,然后设置ItemsSource="{Binding AllImages}"

【讨论】:

  • 我试过了,但它没有解决任何问题 - 似乎根本没有任何区别。我已经尝试设置一个静态过滤器而不从文本框中进行任何更新(只是为过滤器提供一个字符串),当我单步执行我的代码时,它似乎确实设置了过滤器 - 因为它返回 false 和 true 取决于它是否匹配过滤器标准 - 但是显示的结果不反映任何过滤。我觉得它再次变得更加明显,就像我根本没有绑定到正确的东西,或者我没有正确设置 CollectionViewSource 或其他东西。
猜你喜欢
  • 2013-07-18
  • 1970-01-01
  • 2016-04-07
  • 1970-01-01
  • 2012-05-15
  • 2010-11-16
  • 2018-03-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多