MVVM依赖项属性未将值传递给viewmodel

时间:2014-08-22 14:39:09

标签: c# wpf mvvm

我有一个带有两个属性的ViewModel:

    /// <summary>
    /// Gets, sets the start datetime
    /// </summary>
    public DateTime StartDateTime
    {
        get
        {
            return startDateTime;
        }

        set
        {
            if (startDateTime == value) {
                return;
            }
            startDateTime = value;
            RaisePropertyChanged("StartDateTime");
        }
    }

    /// <summary>
    /// Gets, sets the end datetime
    /// </summary>
    public DateTime EndDateTime
    {
        get
        {
            return endDateTime;
        }

        set
        {
            if (endDateTime == value) {
                return;
            }
            endDateTime = value;
            RaisePropertyChanged("EndDateTime");
        }
    }

在我的视图中,我有一个这样的控件:

 <controls:WinFormsWrapper EndDateTime="{Binding EndDateTime, Mode=TwoWay}" 
                           StartDateTime="{Binding StartDateTime, Mode=TwoWay}" />

我的WinFormsWrapper有两个依赖属性,如下所示:

/// <summary>
    /// The dependency property for StartDateTime
    /// </summary>
    public static readonly DependencyProperty StartDateTimeProperty = DependencyProperty.Register("StartDateTime", typeof(DateTime), typeof(WinFormsWrapper), new FrameworkPropertyMetadata(control.StartDateTime, new PropertyChangedCallback(StartDateTime_Changed)));

    /// <summary>
    /// Handles the event
    /// </summary>
    /// <param name="sender">The sender</param>
    /// <param name="e">The arguments</param>
    private static void StartDateTime_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        control.SetDateTimeStartValue((DateTime)e.NewValue, true);
    }

    /// <summary>
    /// Gets, sets the StartDateTime
    /// </summary>
    public DateTime StartDateTime
    {
        get { return (DateTime)GetValue(StartDateTimeProperty); }
        set { SetValue(StartDateTimeProperty, value); }
    }

    /// <summary>
    /// The dependency property for EndDateTime
    /// </summary>
    public static readonly DependencyProperty EndDateTimeProperty = DependencyProperty.Register("EndDateTime", typeof(DateTime), typeof(WinFormsWrapper), new FrameworkPropertyMetadata(control.EndDateTime, new PropertyChangedCallback(EndDateTime_Changed)));

    /// <summary>
    /// Handles the event
    /// </summary>
    /// <param name="sender">The sender</param>
    /// <param name="e">The arguments</param>
    private static void EndDateTime_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        control.SetDateTimeEndValue((DateTime)e.NewValue, true);
    }

    /// <summary>
    /// Gets, sets the EndDateTime
    /// </summary>
    public DateTime EndDateTime
    {
        get { return (DateTime)GetValue(EndDateTimeProperty); }
        set { SetValue(EndDateTimeProperty, value); }
    }

由于某些原因,当我更改gui中的日期时间时,我的viewmodel中的StartDateTime属性发生了更改,但是EndDateTime没有(在WinFormsWrapper类中两个属性都设置正确)。任何人都可以给我一个提示,为什么会这样?

编辑:

经过一些分析后,我认为此事件可能存在问题:

我的WinFormsWrapper : WinFormsHost继承自WinFormsHost。托管的Forms控件有一个CurrentTimeChanged事件,在我WinFormsHost的构造函数中,我正在注册此事件:

public WinFormsWrapper()
    {
        this.Child = control;           

        control.CurrentTimeChanged += control_CurrentTimeChanged;
    }

然后当这个事件发生时我会这样做:

void control_CurrentTimeChanged(object sender, EventArgs e)
    {
        EndDateTime = ((EtDateTimeRange)sender).EndDateTime;            
        StartDateTime = ((EtDateTimeRange)sender).StartDateTime;
    }

似乎我首先设置的任何属性(EndDateTimeStartDateTime)都传递给我的VM,另一个不是。

0 个答案:

没有答案