NotifyPropertyChanged when binding to itemsource of datagrid in Silverlight RIA
I have a textbox and a datagrid in a silverlight project. The textbox should be enabled if the item count in the datagrid is 0 or the sum of a field in the datagrids itemssource = 0.
I've bound the isEnabled value of the textbox to the datagrids ItemsSource.SourceCollection which gives me an IEnumerable. I've made a converter that converts this datamodel to bool.
When I open my silverlight pa开发者_JAVA百科ge and bind the datagrid, the converter runs and everything i working as expected, but nothing happens if i change the sum field or add/delete rows in the datagrid.
I'm guessing it has something to do with notify property changes on my datamodel, but I don't know.
Any thoughts on how to solve this?
I asked similar question and as Luc answered you need have INotifyPropertyChanged event implementation, if not item changes will not happen. How to make a cell in a datagrid readonly based the content on another cell in SL4?
Yes, when you bind to a sub property of an object then you need a PropertyChanged event of that specific property in order for the target to update its value.
In your example the ItemsSource needs to raise a PropertyChanged event of the property SourceCollection.
What you could do is bind to ItemsSource wich will be triggered and then in your converter use the Sourcecollection value.
eg:
<sdk:DataGrid Name="dg" ItemsSource="{Binding}" AutoGenerateColumns="True" VerticalAlignment="Top"/>
<TextBox Text="{Binding ElementName=dg, Path=ItemsSource.Count}" VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
code:
_items = new ObservableCollection<SomeClass>();
_items.Add(new SomeClass() { Name = "a" });
_items.Add(new SomeClass() { Name = "b" });
_items.Add(new SomeClass() { Name = "c" });
DataContext = _items;
private void testButton_Click(object sender, RoutedEventArgs e)
{
_items.Add(new SomeClass(){Name = "ha"});
}
精彩评论