views:

120

answers:

2

Hi, I have a scenario where i load an ICollectionView in a datagrid.

In some cases I modify the data where the collectionview gets it's data from. If I then reload the grid with configGrid.ItemsSource = configData; for example, the data gets updated.

Now the thing is, I sometimes open a new window using:

var newWindow = new Edit(movie);
newWindow.Show();

The thing is, I also edit the data using this new window. Now I want the datagrid in the first window to be refreshed after I close this second window (actually, it doesn't matter when it gets refreshed, as long as it does).

How do I do this?

+1  A: 

I might be missing something here (I have a crippling hangover unfortunately) but can't you handle the window closed event of newWindow and refresh confiGrids itemsource there?

Window newWindow = new Window();
newWindow.Closed += new EventHandler(newWindow_Closed);
newWindow.Show();

void newWindow_Closed(object sender, EventArgs e)
    {
        configGrid.ItemsSource = configData;
    }
MoominTroll
This doesn't seem to work, the event is not being triggered if i close the new window
WtFudgE
Oh. I don't really understand why the above code doesn't work - I've just punched it into a dummy program and the event gets raised just fine. It looks like you're using VB (which I'm not really familiar with) and not C#, so that could be an issue I suppose (although I'd expect event handling to be similar in both languages). Do you have anymore code thats supposed to interact with opening or closing the window?
MoominTroll
Oh, I seemed to have just put the code in a wrong window :PIt works fine, ur right I'm sorry!It's not vb btw :)
WtFudgE
Heh I meant might be, not are :p Was just looking for anything at all as to why it wasnt working, but glad its ok now :)
MoominTroll
if you got the right right answer can you please select the accepted answer button, cheers
harryovers
A: 

If the collection behind the ICollectionView supports INotifyCollectionChanged (like ObservableCollection) and the object itself supports INotifyPropertyChanged then the grid is supposed to update automatically

Otherwise you are on your own and the editing window should raise some sort of notification (maybe an event) that you should receive and update the list.

Ok, here's the long version:

WPF data-binding can update the UI automatically - but it needs to know that something changed in order to trigger the update, the easiest way to do this is to support INotifyPropertyChanged, let's create simple class:

public class Movie
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
}

Now, let's add INotifyPropertyChanged support:

public class Movie : INotifyPropertyChanged
{
    public event PropertyChanged;

    protected virtual OnPropertyChanged(string property)
    {
        var ev = PropertyChanged;
        if(ev!=null)
        {
            ev(this, new PropertyChangedEventArgs(property));
        }
    }

    private string _name;
    public string Name
    {
        get { return _name; }
        set 
        {
            _name = value; 
            OnPropertyChanged("Name");
        }
    }
}

Now when you bind to the movie class and change the Name property the UI will be updated automatically.

The next step is to handle a list of Movie objects, we do that by using a collection class the implements INotifyCollectionChanged, luckily for us there's one already written in the framework called ObservableCollection, you user ObservableCollection<T> the same way you would use a List<T>.

So, just bind to ObservableCollection and WPF will automatically detect when objects change or when they are added or removed.

ICollectionView is very useful, it adds support for current item, sorting, filtering and grouping on top of the real collection, if that collection is an ObservableCollection everything will just work, so the code:

ObservableCollection<Movie> movies = new ObservableCollection<Movie>();
ICollectionView view = CollectionViewSource.GetDefaultView(movies);

will give you a collection view that supports automatic change notifications.

Nir
I'm not very familiar with all these databinding tecniques, so maybe I should use something else instead of ICollectionView?
WtFudgE
I've updated my answer to include all the details you need to use those techniques, hope this helps.
Nir
Thanks, it had to be tuned a little, but i got it to work :)
WtFudgE