WPF binding not updating until after another action

Posted by Matthew Stanford on Stack Overflow See other posts from Stack Overflow or by Matthew Stanford
Published on 2010-05-20T14:07:35Z Indexed on 2010/05/20 14:40 UTC
Read the original article Hit count: 413

I have an observable collection bound to a listbox in WPF. One of the options in the window is to use an OpenFileDialog to add an item to the listbox with certain properties. When I use the OpenFileDialog it immeditaely sets two of the properties of the new item in the observable collection. I am using INotifyPropertyChanged to update the listbox. These two new properties are set correctly and now the listbox should display the title contained in the new title property and the title textbox which is bound to the listbox should display the new title as well. However, neither displays the new title upon the closing of the OpenFileDialog and when I click on another item in the listbox and come back to the item I have just changed it updates the title textbox but the title displayed in the list box is not changed until i move the item in the list box that I want to change.

Here is the Binding code.

ItemsSource="{Binding Path=MyData, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

And here is the implementation of the browse button that is not working (L1 being the listbox)

        private void browse_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog opf = new OpenFileDialog();
        opf.ShowDialog();
        MyData[L1.SelectedIndex].Title = System.IO.Path.GetFileNameWithoutExtension(opf.FileName);
        MyData[L1.SelectedIndex].Command = opf.FileName;
    }

When I simply type in the text boxes and click out of them it updates the list box immediately with the new information I have put in. I also have a create new button and upon clicking it, it immediately adds a new item to the list box and updates its' properties. The only one that is not updating correctly is this peice of code I have given you. Thanks for your help.

EDIT:

Here is my implementation of INotifyPropertyChanged

 private OCLB _MyData; 
    public OCLB MyData
    { 
        get
        {
            return  _MyData;
        }

        set
        {
            _MyData= value;
            FirePropertyNotifyChanged("MyData");
        }
    }

OCLB is the obserable collection. Here is the function FirePropertyNotifyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    private void FirePropertyNotifyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

Each of these are in the partial class MainWindow for the wpf form. I also have a class for the MyData files (with 4 get/set functions) that are stored in the OCLB(observable collection). There is also a class with functions for the OCLB.

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf