I have just started to create a user control realising that I want a lot more out of the DataGrid. Don't worry about the details for now but I have created a DependencyProperty called TheColumns :
public static readonly DependencyProperty TheColumnsProperty = DependencyProperty.Register("TheColumns", typeof(List<DataGridColumnItem>), typeof(ExtendedDataGrid), null);
DataGridColumnItem
is just an independent class I created to hold column information etc.
I then declared a property to get and set the value :
public List<DataGridColumnItem> TheColumns
{
get
{
return (List<DataGridColumnItem>)GetValue(TheColumnsProperty);
}
set
{
SetValue(TheColumnsProperty, value);
TheDataGrid.Columns.Clear();
foreach (var col in TheColumns)
{
var newCol = new DataGridTextColumn();
newCol.Header = col.Header;
var columnBinding = new Binding(col.DataBindingMember);
newCol.Binding = columnBinding;
TheDataGrid.Columns.Add(newCol);
}
}
}
OK, this is all a simple test before I do it properly becase I wanted to set my columns from a view model, as there is no proper column binding in the dataGrid itself. Oh, I want to determin the colum order, visibility and what is actually going to be displayed at run time, not in xaml.
I have a property in my view model called GridColumns declared as :
public List<DataGridColumnItem> GridColumns
....
I have boud this property in xamle like :
TheColumns="{Binding Path=GridColumns}"
OK, now I created an instance of the view model and set it to the DataContext of the Window in which the user control is housed. When I run it, the property IS read from the view model but the setter on the dependency property does not fire. If I remode the xaml assignment the property on the view model does not fire. Therefore, the property is being read but not made use of.