WPF 自定义事件处理程序
WPF Custom Event Handler
我已经实现了 DatePicker https://github.com/cmyksvoll/HighlightDatePicker
的代码,但我无法在 WPF 中使用 SelectedDateChanged 并出现错误 ArgumentException: Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type
我尝试为“SelectedDateChanged”创建自定义偶数处理程序,但 HighlightDatePicker class 是静态的,我无法注册它以便在 MainWindow
中调用我的方法
WPF:
<hdp:HighlightDatePicker SelectedDate="{Binding SelectedDate}" HighlightedDates="{Binding HighlightedDates}" HighlightBrush="LawnGreen" SelectedDateChanged="ProviderDateChanged"/>
我要调用的 MainWindow 方法:
public void ProviderDateChanged(object Sender, RoutedEventArgs e)
{ // fetch data to display }
HighlightedDatePickerClass:
public class HighlightDatePicker : DatePicker
{
static HighlightDatePicker()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(HighlightDatePicker), new FrameworkPropertyMetadata(typeof(HighlightDatePicker)));
public static readonly DependencyProperty HighlightedDatesProperty = DependencyProperty.Register("HighlightedDates", typeof(IList<HighlightedDate>), typeof(HighlightDatePicker));
public IList<HighlightedDate> HighlightedDates
{
get { return (IList<HighlightedDate>)GetValue(HighlightedDatesProperty); }
set { SetValue(HighlightedDatesProperty, value); }
}
public static readonly DependencyProperty HighlightBrushProperty = DependencyProperty.Register("HighlightBrush", typeof(Brush),
typeof(HighlightDatePicker), new PropertyMetadata(Brushes.Orange));
public Brush HighlightBrush
{
get { return (Brush)GetValue(HighlightBrushProperty); }
set { SetValue(HighlightBrushProperty, value); }
}
}
public class HighlightedDate
{
public HighlightedDate(DateTime date, string description)
{
Date = date;
Description = description;
}
public DateTime Date { get; set; }
public string Description { get; set; }
}
SelectedDateChanged 事件的处理程序方法的正确签名是:
private void ProviderDateChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
// fetch data to display
}
我已经实现了 DatePicker https://github.com/cmyksvoll/HighlightDatePicker
的代码,但我无法在 WPF 中使用 SelectedDateChanged 并出现错误 ArgumentException: Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type
我尝试为“SelectedDateChanged”创建自定义偶数处理程序,但 HighlightDatePicker class 是静态的,我无法注册它以便在 MainWindow
中调用我的方法WPF:
<hdp:HighlightDatePicker SelectedDate="{Binding SelectedDate}" HighlightedDates="{Binding HighlightedDates}" HighlightBrush="LawnGreen" SelectedDateChanged="ProviderDateChanged"/>
我要调用的 MainWindow 方法:
public void ProviderDateChanged(object Sender, RoutedEventArgs e)
{ // fetch data to display }
HighlightedDatePickerClass:
public class HighlightDatePicker : DatePicker
{
static HighlightDatePicker()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(HighlightDatePicker), new FrameworkPropertyMetadata(typeof(HighlightDatePicker)));
public static readonly DependencyProperty HighlightedDatesProperty = DependencyProperty.Register("HighlightedDates", typeof(IList<HighlightedDate>), typeof(HighlightDatePicker));
public IList<HighlightedDate> HighlightedDates
{
get { return (IList<HighlightedDate>)GetValue(HighlightedDatesProperty); }
set { SetValue(HighlightedDatesProperty, value); }
}
public static readonly DependencyProperty HighlightBrushProperty = DependencyProperty.Register("HighlightBrush", typeof(Brush),
typeof(HighlightDatePicker), new PropertyMetadata(Brushes.Orange));
public Brush HighlightBrush
{
get { return (Brush)GetValue(HighlightBrushProperty); }
set { SetValue(HighlightBrushProperty, value); }
}
}
public class HighlightedDate
{
public HighlightedDate(DateTime date, string description)
{
Date = date;
Description = description;
}
public DateTime Date { get; set; }
public string Description { get; set; }
}
SelectedDateChanged 事件的处理程序方法的正确签名是:
private void ProviderDateChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
// fetch data to display
}