开发者

WPF DataGrid binding

Hi all i am new to WPF and i have a small problem ,i hope you can help me.

I have a DataGrid in a parent window..when i open a pop up window and select an item,that pop up window closes and the DataGrid in the parent window updated with the selected item.

My problem is when i set the DataGrid's ItemSource property,it works only in the first time,but when i add other items and set the DataGrid's ItemSource it does not update开发者_C百科 the Grid though the List it is bound to,is updated.

I wonder if there is a method for DataBind() like the ASP.Net GridView


In WPF your class typically needs to implement INotifyPropertyChanged. This interface exposes the PropertyChanged event, which 'notifies' when a property has changed.

Here's an example from CodeProject

// Declare the PropertyChanged event
public event PropertyChangedEventHandler PropertyChanged;

// OnPropertyChanged will raise the PropertyChanged event passing the
// source property that is being updated.
private void OnPropertyChanged(object sender, string propertyName)
{
    if (this.PropertyChanged != null)
    {
        PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
    }
}

You typically call a method like this inside the setter for a binding property.

You can use an ObservableCollection, as Henk suggests, because the ObservableCollection implements this interface and has built in notification on changes (add, remove, etc). Note that creating a new ObservableCollection won't raise a notification

this.BindingCollection = new ObservableCollection(myList);

so you will have to raise the notification yourself

OnPropertyChanged(this, "BindingCollection");


What kind of List are you using?

It ought to work as you described when using an ObservableCollection.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜