I have the following custom datagrid column, for passing in the value of the DatePart
dependency property as the ConverterParameter
to the editing element's converter:
Public Class DataGridTimeColumn
Inherits DataGridTextColumn
Shared ReadOnly DatePartProperty = DependencyProperty.Register("DatePart", GetType(DateTime?), GetType(DataGridTimeColumn), New FrameworkPropertyMetadata(Nothing, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, AddressOf RefreshBinding))
Property DatePart As DateTime?
Get
Return GetValue(DatePartProperty)
End Get
Set(value As DateTime?)
SetValue(DatePartProperty, value)
End Set
End Property
Private Shared Sub RefreshBinding(d As DependencyObject, e As DependencyPropertyChangedEventArgs)
Dim tc As DataGridTimeColumn = d
tc.Binding = tc.Binding
End Sub
Public Overrides Property Binding As BindingBase
Get
Return MyBase.Binding
End Get
Set(value As BindingBase)
Dim b As Data.Binding = value
With b
.Converter = New TimeConverter
.ConverterParameter = DatePart
End With
MyBase.Binding = b
End Set
End Property
End Class
With the following XAML:
<my:DataGridTimeColumn Header="From" Binding="{Binding FromDate}" DatePart="{Binding FromDate}" />
<my:DataGridTimeColumn Header="Until" Binding="{Binding TillDate}" DatePart="{Binding TillDate}" />
But RefreshBinding
is never called (I've set a breakpoint and it's never triggered), and thus DatePart
is always Nothing
(null
) when the ConverterParameter
is set. How can I fix this?
Edit
In C#:
public class DataGridTimeColumn : DataGridTextColumn
{
static readonly DependencyProperty DatePartProperty = DependencyProperty.Register(
"DatePart", typeof(DateTime?), typeof(DataGridTimeColumn),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, RefreshBinding)
);
public DateTime? DatePart
{
get { return (DateTime?)GetValue(DatePartProperty); }
set { SetValue(DatePartProperty, value); }
}
private static void RefreshBinding(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var tc = (DataGridTimeColumn)d;
tc.Binding = tc.Binding;
}
public override System.Windows.Data.BindingBase Binding
{
get { return base.Binding; }
set
{
var b = (Binding)value;
b.Converter = new TimeConverter();
b.ConverterParameter = DatePart;
base.Binding = b;
}
}
}