简体   繁体   中英

Problems with data binding on dependency properties

I'm getting an exception telling me "Binding' cannot be set on the 'InitialStartDateTime' property of type 'WinFormsWrapper'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject." but my properties are dependency properties as you can see here, right?

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

and this is my xaml:

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

Thank you in advance!

EDIT: Thanks alot, its running now, but it's still not displaying my control, any ideas why?

You are using the wrong properties in your DependencyProperty creation.

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

In the Register part, it should have been InitialEndDateTime instead of EndDateTime .

 DependencyProperty.Register("StartDateTime", 

You have to Name it

 DependencyProperty.Register("InitialStartDateTime", 

:)

Same Problem with the EndDateTime

I hope that solves your problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM