使用Dependency属性传递回调方法

dot*_*ixx 3 c# wpf xaml calendar attached-properties

我正在使用Jarloo的日历控件,以便在WPF软件中显示日历。为了我的需要,我补充说每天都有一个项目列表,可以说List<Item> Items

Jarloo日历是我的主要Visual Studio解决方案中的第二个项目。我以这种方式使用此控件:

<Jarloo:Calendar DayChangedCallback="{Binding DayChangedEventHandler}"/>
Run Code Online (Sandbox Code Playgroud)

如您所见,我希望可以将一个方法从主项目传递到日历的项目,以便可以在Calendar的构造函数中将该方法添加为DayChanged事件的事件处理程序。

但是,通过依赖项收到的项目为null ...

在日历代码中,我的依赖项属性定义为:

public static readonly DependencyProperty DayChangedCallbackProperty = DependencyProperty.Register("DayChangedCallback", typeof(EventHandler<DayChangedEventArgs>), typeof(Calendar));
Run Code Online (Sandbox Code Playgroud)

我的“ DayChangedEventHandler”定义为

public EventHandler<DayChangedEventArgs> DayChangedHandler { get; set; }
void DayChanged(object o, DayChangedEventArgs e)
{
}

// i set this way the DayChangedHandler property so that I can bind on it from the view
DayChangedHandler = new EventHandler<DayChangedEventArgs>(DayChanged);
Run Code Online (Sandbox Code Playgroud)

有人对我有提示吗?

非常感谢:) .x

Ola*_*cea 5

这是关于您的非静态字段问题的示例:

public partial class MainWindow : Window
{
   public bool IsChecked
   {
       get { return (bool)GetValue(IsCheckedProperty); }
       set { SetValue(IsCheckedProperty, value); }
   }

// Using a DependencyProperty as the backing store for IsChecked.  This enables animation, styling, binding, etc...
   public static readonly DependencyProperty IsCheckedProperty =
    DependencyProperty.Register("IsChecked", typeof(bool), typeof(MainWindow), new PropertyMetadata(false, new PropertyChangedCallback(PropertyChanged)));


   private static void PropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
   {
       MainWindow localWindow = (MainWindow)obj;
       Console.WriteLine(localWindow.TestString);
    }

   public string TestString { get; set; }

    public MainWindow()
    {
       InitializeComponent();

       TestString = "test";
       this.DataContext = this;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是XAML进行测试:

<CheckBox Content="Case Sensitive" IsChecked="{Binding IsChecked}"/>
Run Code Online (Sandbox Code Playgroud)

更改属性后,将调用回调,在第十四示例中,您可以访问非静态的TestString属性。