开发者

WPF datagrid multiple windows question

开发者 https://www.devze.com 2022-12-12 17:14 出处:网络
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 =

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开发者_JAVA百科, 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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消