I'm currently attempting to create a custom control out of a DataGridTemplateColumn
that will be reused across many of our applications. I'm running into some issues getting a dependency property on the custom control to bind and raise the property changed notification correctly.
I currently have the control inheriting from DataGridTemplateColumn
the xaml looks like this:
<DataGridTemplateColumn x:Class="Controls.DataGridDateColumn"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Text="{Binding SelectedDate}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<Grid FocusManager.FocusedElement="{Binding ElementName=DatePicker}">
<DatePicker Name="DatePicker" HorizontalAlignment="Left" VerticalAlignment="Center" SelectedDate="{Binding SelectedDate}"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
And the code behind looks like this
public partial class DataGridDateColumn : DataGridTemplateColumn
{
public static readonly DependencyProperty SelectedDateProperty =
DependencyProperty.Register("SelectedDate",
typeof(DateTime?),
typeof(DataGridDateColumn),
new FrameworkPropertyMetadata(null, OnSelectedDateChanged));
private static void OnSelectedDateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
DataGridDateColumn col = (DataGridDateColumn)d;
col.SelectedDate = (DateTime?)e.NewValue;
}
public DateTime? SelectedDate {
get {
return (DateTime?)GetValue(SelectedDateProperty);
}
set {
SetValue(SelectedDateProperty, value);
}
}
public DataGridDateColumn()
{
InitializeComponent();
}
}
When I have the control inside of my data grid on the main page and attempt to bind to SelectedDate like this <Controls:DataGridDateColumn Header="Policy Date" SelectedDate="{Binding Path=PolicyDate}" SortMemberPath="PolicyDate" />
I'm getting a binding error in the output window that states that it can't find the dependency property I'm referring to
System.Windows.Data Error: 40 : BindingExpression path error: 'SelectedDate' property not found on 'object' ''TestData' (HashCode=32071430)'. BindingExpression:Path=SelectedDate; DataItem='TestData' (HashCode=32071430); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
My initial thought is that because this is an items control I need to register the dependency property differently than I am, but I can't find any additional information.
The reason I am attempting to create custom columns is because we are planning on having specific behaviors associated with a few different types of the columns in order to make the user experience more homogeneous across all of our apps. So I want to be able to handle the behavior inside of the custom control so that we don't have to constantly hook up the different events to the template on every data grid that uses it.
Any suggestions would be appreciated.