Silverlight - property rework?
I am just getting started with silverlight. Basically I have a silverlight custom control that has various dataGrids and combobox, their sources sources are set in the properties of a regular regular C # object. My problem is that I have a dropdown that when the user selects an item from the list, a new row appears in one of the grids. All I do is handle the SelectionChanged event and add a new item to the list in my custom object and set the item source for the grid again. It doesn't seem to work; no row is added to the dataGrid I don't know how to get my grid to "re-validate" this property. I read about dependency properties, is this what I need?
Any pointers would be really appreciated.
a source to share
The problem is that when you assign the same list to ItemsSource
, DataGrid
knows its the same list so that it doesn't do anything.
As Henrik points out, you should expose Observable<T>
not a List<T>
for properties that need to be bound to ItemsSource
properties of multiple controls, for example DataGrid
, ListBox
etc.
In addition, your "plain C # objects" must implement the interface INotifyPropertyChanged
if you want the changes made by code to those properties to be automatically reflected in the user interface.
a source to share
What you probably want to do is update the binding source - this is relatively easy to do.
private void ComboBox_SelectionChanged(object sender, RoutedEventArgs e)
{
this.dataGrid.GetBindingExpression(DataGrid.ItemsSource).UpdateSource();
}
It's a little hacky but will do what you need it to. Implementation INotifyPropertyChanged
is another great suggestion.
The Silverlight show has some great info about INotifyPropertyChanged
here
a source to share