views:

167

answers:

1

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 observable 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.

+3  A: 

I think what you're seeing here is that the ObservableCollection is not firing the PropertyChanged event on an item in your collection. The parent form isn't getting any notifications until you scroll to the item in question.

The reason your new button is working is act of adding a new item to the collection which fires the CollectionChanged event on the ObservableCollection.

In order to get your collection to update, you'll need to implement a suitable property changed mechanism on the items within the collection. For example, if you are creating, ObservableCollection<MyDataItem>, MyDataItem needs to properly implement INotifyPropertyChanged.

Hope that helps,

SergioL
I have added my implementation of INotifyPropertyChanged to my original question now so I can find out what is wrong with it.
mjstide
MyData appears to be your collection. The class containing the properties "Title" and "Command" need the INotifyPropertyChanged treatment.
SergioL
I am deserializing an XML to a container. That container is MyData. MyData is an ObservableCollection<MyClass>. MyClass contains the title and command properties. Are you saying that I need to put the INotifyPropertyChanged functions in MyClass? Or do I need to change to an INotifyCollectionChange?
mjstide
Alright I got it to work by changing to location of the openfiledialog to where INotifyCollectionChange was updating in the collection class thanks for the help.
mjstide
Yes, MyClass would need to implement INotifyPropertyChanged and the two properties, Title and Command, would need to fire the event in their setters.
SergioL