How to update DataGrid in Silverlight when Property in ObserveableCollection changed
I have a Datagrid bound to a ObserveableCollection(MyClass) source1;
MyClass has 2 Properties: string Name, int AGE
Now i have 50 MyClass Objets in开发者_如何学JAVA the Collection, that means i have 50 rows in my datagrid. if i want to see all rows, i have to scroll around and that is ok!!
private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e){
int index = dataGrid.SelectedIndex;
obsCollection[index].Name="AAAAA";
}
Everytime i click on a row, i would like that on that row Name is changed to string Name="AAAAAA";
The whole thing works if i scroll up or down and the row is not anymore shown in the datagrid. Somehow the row gets updatet when it is out of sight and later shown. When i scroll around and come back to that row and the row is now SHOWN again in datagrid, the value has been updatet.
But i want an instant change!! Just select/click on the row and Name is changed to "AAAAAA". I do not want to have that row out of sight, to get an update.
edit: i can not use datagrid.itemsssource=null; because i would get infinite loop on selectionchanged
Your generic type of ObservableCollection need to implement INotifyPropertyChanged. For example you have collection of Employee
and want the UI to update automatically when some employee's value changed.
You need to create Employee class and implement INotifyPropertyChanged.
public class Employee : INotifyPropertyChanged { public string FirstName { get { return this._firstName; } set { this._firstName = value; this.NotifyPropertyChanged("FirstName"); } } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } }
Use Employee as generic parameter type to ObservableCollection like this
ObservableCollection<Employee>
Now, when you change value of employee in ObservableCollection, the value will update to UI for you.
精彩评论