对依赖项属性的数据绑定问题

时间:2014-08-11 10:35:47

标签: c# wpf

我得到一个例外,告诉我“Binding”不能在'WinFormsWrapper'类型的'InitialStartDateTime'属性上设置。'Binding'只能在DependencyObject的DependencyProperty上设置。“但我的属性是依赖属性,你可以在这里看到,对吧?

public class WinFormsWrapper : WindowsFormsHost
{       
    /// <summary>
    /// The control element
    /// </summary>
    private static EtDateTimeRange control = new EtDateTimeRange();     

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

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

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

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

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

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

这是我的xaml:

<controls:WinFormsWrapper InitialStartDateTime="{Binding StartDateTime}" InitialEndDateTime="{Binding EndDateTime}" />

提前谢谢!

编辑:非常感谢,它现在正在运行,但它仍然没有显示我的控件,任何想法为什么?

2 个答案:

答案 0 :(得分:3)

您在DependencyProperty创建中使用了错误的属性。

public static readonly DependencyProperty InitialEndDateTimeProperty = DependencyProperty.Register("EndDateTime", typeof(DateTime), typeof(WinFormsWrapper), new FrameworkPropertyMetadata(control.InitialEndDateTime, new PropertyChangedCallback(InitialEndDateTime_Changed)));

在注册部分,它应该是InitialEndDateTime而不是EndDateTime

答案 1 :(得分:2)

 DependencyProperty.Register("StartDateTime", 

你必须命名

 DependencyProperty.Register("InitialStartDateTime", 

:)

与EndDateTime相同的问题

我希望能解决你的问题