ICollectionView Filter won't refresh - are my bindings wrong?

Beginning to learn C#/XAML, so very well may not have done this the right way. I'm writing a program to view images, list them by their file name, and have a text box for searching that automatically filters the list as you type in it.

I have ran through my C# code numerous times and I can see the filter actually working - and it looks as if the refresh is firing too - however I cannot visually get it to filter or 'refresh' on the screen.

the C# for the box is as follows

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();
        };
    }
}

and the xaml is

<TextBox x:Name="TextBoxFilter"/>

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

I have a feeling the problem is in the list box binding, but I can't nut out the real root cause. Another thing perhaps noteable is i'm holding a string and imagesource within each object in my list.

Any help is much appreciated!

EDIT: Below is the code i use to populate AllImages - using Environment.SpecialFolder.MyPictures as a sample. 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;
        }
    }

Answers


Your instincts are on track, the problem originates from your bindings. Your ItemsSource is not bound to the same view instance your filter is using. Try it this way:

    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"/>

Need Your Help

accessing android internal storage with cordova file plugin

android cordova phonegap-plugins cordova-plugins

I'm trying to make an android app that, so far, is using the native voice recorder to record audio.

How to port code using Windows conio.h to Linux?

c include conio

I wrote this C program for Win32/c compiler but while i'm trying run this using gcc in Linux machine or codepad.org it shows 'conio.h: No such file or directory compilation terminated' What are

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.