Reputation: 13
I am creating a column of bound DatePicker controls in a DataGrid control. For each of the DatePicker controls I would like to attach a SelectedDateChangedEvent handler, but I am having difficulty doing this.
Consider the following code for WPF:
private void dg_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyName == "Event Date")
{
var f = new FrameworkElementFactory(typeof(DatePicker));
...
// I tried this:
//f.AddHandler(DatePicker.SelectedDateChangedEvent, new RoutedEventHandler(aRoutedEventHandler));
// and I tried this:
//f.AddHandler(DatePicker.SelectedDateChangedEvent, new SelectionChangedEventHandler(aSelectionChangedEventHandler));
e.Column = new DataGridTemplateColumn()
{
Header = e.Column.Header,
CellTemplate = new DataTemplate() { VisualTree = f },
};
}
}
private void aRoutedEventHandler(object sender, RoutedEventArgs e) { ... }
private void aSelectionChangedEventHandler(object sender, SelectionChangedEventArgs e) {...}
Trying either of the event handlers result in an "Argument Exception":
A first chance exception of type 'System.ArgumentException' occurred in PresentationFramework.dll Additional information: Handler type is not valid.
What is the correct handler to use?
Upvotes: 0
Views: 534
Reputation: 63
You need to add the handler as follows for the DatePicker SelectedDateChangedEvent
f.AddHandler(DatePicker.SelectedDateChangedEvent, new EventHandler<SelectionChangedEventArgs>(aSelectionChangedEventHandler));
The handler type can be found by looking into the DatePicker class at the public events declared
public event RoutedEventHandler CalendarOpened;
public event RoutedEventHandler CalendarOpened;
public event EventHandler<DatePickerDateValidationErrorEventArgs>
public event EventHandler<SelectionChangedEventArgs> SelectedDateChanged;
Upvotes: 1