So I've just implemented my first usercontrol. It holds a ComboBox that I want to populate with checkboxes. It is going to sit repeatedly in the headers of a datagrid.
The ItemsSource of the combobox is exposed via a DependencyProperty in my usercontrol. When I declare this DependecyProperty I also pass a callback because I want to prepare the collection before it gets passed to the combobox. This is done in the callback.
Everything works fine and the bindings seem to work, except that they do not get updated when the underlying collection changes. My underlying collection is an ObservableCollection and my callback only gets called when the collection is declared (eventhough I have propertychanged call for the property right after I added my items).
This is my code for the usercontrol:
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource",typeof(ObservableCollection<RoomViewModel>),typeof(DataGridFilterControl),new PropertyMetadata(new PropertyChangedCallback(ItemsSourceChangedCallBack)));
static private void ItemsSourceChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
DataGridFilterControl originator = d as DataGridFilterControl;
if (originator != null)
{
originator.PopulateCheckBoxes();
}
}
public ObservableCollection<RoomViewModel> ItemsSource
{
get
{
return (ObservableCollection<RoomViewModel>)GetValue(ItemsSourceProperty);
}
set
{
SetValue(ItemsSourceProperty,value);
}
}
And the xaml for the ComboBox:
<ComboBox ItemsSource="{Binding Path=CheckBoxView}" />
As you can see the ItemsSource for the ComboBox is different from the DependecyProperty. This is on purpose. However this should not affect my callback, right?
Im adding the items to the collection from another thread and therefore do it inside an Dispatcher using the Invoke method (if that makes any difference).
How can I make my Callback get called when the Property is changed or I invoke propertychanged?