PropertyChangeCallBack 未在 UserControl 中的 DependencyProperty 上触发

发布于 2024-11-08 16:03:50 字数 2464 浏览 3 评论 0原文

困惑:我正处于创建月份计划控件的初始阶段,该控件需要执行一些 UI 操作,当从 XAML 中绑定的 ViewModel 提供日期时,该控件。当我运行代码时,ViewModel 确实返回值,但 PropertyChangeCallBack 没有触发。我在 public class ExtendedDataGrid : DataGrid 控件中做了完全相同的事情,并且工作正常,但几乎就像您必须对 UserControls 做一些不同的事情。这是我的代码的基础:

public partial class DiaryMonthViewUserControl : UserControl
{
    private DateTime _topDate;


    public DiaryMonthViewUserControl()
    {
        InitializeComponent();
    }

    // Property to get the date from the ViewModel for processing. This will fire and event to set the month we want to display
    public static readonly DependencyProperty TheDateProperty = DependencyProperty.Register("TheDate", typeof(DateTime),
                      typeof(DiaryMonthViewUserControl),
                      new FrameworkPropertyMetadata(DateTime.Today, OnDatePropertyChanged, null));

    public void SetTheCurrentDate(DateTime CurrentDate)
    {
        _topDate = CurrentDate;

        // Like Outlook, whatever date we supply, I want the first top level displayed date for the month
        if (_topDate.Day > 1)
            _topDate = _topDate.AddDays(-(_topDate.Day-1)); // First day of the month

        // Get to the first Monday before the 1st of the month if not on a Monday
        while (_topDate.DayOfWeek != DayOfWeek.Monday)
            _topDate = _topDate.AddDays(-1);

        // I will set the UI here once I have solved this problem.
        MakeChangesToTheUIPlease();
    }

    private static void OnDatePropertyChanged(DependencyObject Source, DependencyPropertyChangedEventArgs e)
    {
        var control = Source as DiaryMonthViewUserControl;

        if (control != null)
            control.SetTheCurrentDate((DateTime)e.NewValue);
    }

    [Bindable(true)]
    public DateTime TheDate
    {
        get { return (DateTime)GetValue(TheDateProperty); }
        set { SetValue(TheDateProperty, value); }
    }
}

我还尝试使用 new PropertyChangedCallback(OnDatePropertyChanged) 作为参数,但仍然不起作用。

我的绑定如下:

<my:DiaryMonthViewUserControl HorizontalAlignment="Left" Margin="12,12,0,0" x:Name="diaryMonthViewUserControl1" 
                              VerticalAlignment="Top" Height="325" Width="476" TheDate="{Binding Path=CurrentDate}" />

当我运行代码时,我的 ViewModel 在 CurrentDate 的 getter 上中断,如果我删除 CurrentDate 绑定,则它不会中断。问题是回电没有触发,我无论如何也无法理解为什么。

任何帮助将不胜感激,特别是可能涵盖此问题的文章的链接。

Stumped : I am in the initial stages of creating a month planner control that need to carry out some UI action I The control when supplying a date from the ViewModel bound in XAML. When I run the code, the ViewModel does return the value but the PropertyChangeCallBack is not firing. I have done exactly the same thing in an public class ExtendedDataGrid : DataGrid control and that work fine but it is almost like you have to do something different with UserControls. Here is the basis of my code:

public partial class DiaryMonthViewUserControl : UserControl
{
    private DateTime _topDate;


    public DiaryMonthViewUserControl()
    {
        InitializeComponent();
    }

    // Property to get the date from the ViewModel for processing. This will fire and event to set the month we want to display
    public static readonly DependencyProperty TheDateProperty = DependencyProperty.Register("TheDate", typeof(DateTime),
                      typeof(DiaryMonthViewUserControl),
                      new FrameworkPropertyMetadata(DateTime.Today, OnDatePropertyChanged, null));

    public void SetTheCurrentDate(DateTime CurrentDate)
    {
        _topDate = CurrentDate;

        // Like Outlook, whatever date we supply, I want the first top level displayed date for the month
        if (_topDate.Day > 1)
            _topDate = _topDate.AddDays(-(_topDate.Day-1)); // First day of the month

        // Get to the first Monday before the 1st of the month if not on a Monday
        while (_topDate.DayOfWeek != DayOfWeek.Monday)
            _topDate = _topDate.AddDays(-1);

        // I will set the UI here once I have solved this problem.
        MakeChangesToTheUIPlease();
    }

    private static void OnDatePropertyChanged(DependencyObject Source, DependencyPropertyChangedEventArgs e)
    {
        var control = Source as DiaryMonthViewUserControl;

        if (control != null)
            control.SetTheCurrentDate((DateTime)e.NewValue);
    }

    [Bindable(true)]
    public DateTime TheDate
    {
        get { return (DateTime)GetValue(TheDateProperty); }
        set { SetValue(TheDateProperty, value); }
    }
}

I have also tried using new PropertyChangedCallback(OnDatePropertyChanged) as a parameter and that still does not work.

My binding is as follows:

<my:DiaryMonthViewUserControl HorizontalAlignment="Left" Margin="12,12,0,0" x:Name="diaryMonthViewUserControl1" 
                              VerticalAlignment="Top" Height="325" Width="476" TheDate="{Binding Path=CurrentDate}" />

When I run the code my ViewModel breaks on the getter for CurrentDate and if I remove the CurrentDate binding then it does not. The problem is that the call back is not firing and, for the life of me, I cannot fathom why.

Any help would be much appreciated, particularly links to articles that may cover this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲娇萝莉攻 2024-11-15 16:03:50

仅当立即属性发生更改时才会触发 PropertyChanged 回调,这意味着需要替换整个 DateTime 对象,如果 DateTime 对象的属性发生更改,则不会引发该事件。这就是为什么它适用于像 int 这样的基本类型。

如果您想在 DateTime 的任何属性发生更改时执行某些方法,您可以实现一个提供通知的 DateTime 类。然后,只要任何属性发生更改,您就可以执行该方法。

The PropertyChanged callback only fires if the immediate property is changed, that means the whole DateTime object would need to be replaced, if properties of the DateTime object are changed the event will not be raised. This is why it works for primitive types like int.

If you want to execute some method whenever any property of the DateTime changed you could implement a DateTime class which provides notifications. You can then execute the method whenever any of the properties change.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文