简体   繁体   English

WPF 自定义事件处理程序

[英]WPF Custom Event Handler

I have implemented the code for DatePicker https://github.com/cmyksvoll/HighlightDatePicker but I cannot use SelectedDateChanged in WPF with the error ArgumentException: Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type我已经实现了 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

I have tried to create custom Even Handler for "SelectedDateChanged" but the HighlightDatePicker class is static and I cannot register it so that my method will be called in MainWindow我尝试为“SelectedDateChanged”创建自定义偶数处理程序,但 HighlightDatePicker class 是 static,我无法注册它,以便在 MainWindow 中调用我的方法

WPF: WPF:

<hdp:HighlightDatePicker SelectedDate="{Binding SelectedDate}"  HighlightedDates="{Binding HighlightedDates}" HighlightBrush="LawnGreen" SelectedDateChanged="ProviderDateChanged"/>

My MainWindow method to call:我要调用的 MainWindow 方法:

        public void ProviderDateChanged(object Sender, RoutedEventArgs e)
        { // fetch data to display }

HighlightedDatePicker Class:突出显示日期选择器 Class:

    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; }
}

The correct signature for the handler method for the SelectedDateChanged event is: SelectedDateChanged 事件的处理程序方法的正确签名是:

    private void ProviderDateChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        // fetch data to display
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM