gpt4 book ai didi

c# - 使用 Dependency Property 传递回调方法

转载 作者:行者123 更新时间:2023-11-30 14:52:47 25 4
gpt4 key购买 nike

我正在使用 Jarloo's calendar control为了在我的 WPF 软件中显示日历。为了我的需要,我补充说每一天都包含一个项目列表,比方说 List<Item> Items .

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

<Jarloo:Calendar DayChangedCallback="{Binding DayChangedEventHandler}"/>

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

但是,通过依赖接收到的item是null...

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

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

我的“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);

有人给我提示吗?

非常感谢 :) .x

最佳答案

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

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

这是对其进行测试的 XAML:

<CheckBox Content="Case Sensitive" IsChecked="{Binding IsChecked}"/>

当属性改变时,回调被调用,在这个例子中,您可以访问非静态 TestString 属性。

关于c# - 使用 Dependency Property 传递回调方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30939209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com