none
WPF Dependency property issue for usercontrol RRS feed

  • Question

  • Hi,

    In my application, i want to bind an object to a new dependency property which i have created. But i am not able to assign it correctly. If i assign the same object tp DataContext of the UserControl then the DataContext gets the assigned object. 

    Here is my code below, whats wrong with the code below:

            public IElementProvider Data
            {
                get { return (IElementProvider)GetValue(DataProperty); }
                set { SetValue(DataProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty DataProperty =
                DependencyProperty.Register("Data", typeof(IElementProvider), typeof(UserControl1), new PropertyMetadata(new PropertyChangedCallback(SpecialChangedPropertyCallback)));
    
            private static void SpecialChangedPropertyCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                UserControl1 myUserControl = (UserControl1)d;
                // I want to assign the new Dependency Property to some other object here
            }
    
     // My XAML code
    <Views:UserControl1 x:Name="myUserControl1" 
                DataContext="{Binding Path=Parameters}" 
                Data="{Binding DataContext, RelativeSource={RelativeSource Self}}" >

    I tried various binding options for 'Data' property but still could not get it work. Can someone help me?

    Thanks,

    IamHuM

    • Changed type IamHuM Tuesday, December 17, 2013 3:02 PM
    Tuesday, December 17, 2013 2:40 PM

Answers

  • Hi,

    Since you are binding to the DataContext of the same usercontrol. set the Dependency property in the Code-Behind. You can pin point what is the issue.

          public UserControl1()
            {
                InitializeComponent();
                this.Loaded += UserControl1_Loaded;
            }
    
            void UserControl1_Loaded(object sender, RoutedEventArgs e)
            {
                Data = (IElementProvider)((sender as UserControl).DataContext);
            }
    
            public IElementProvider Data
            {
                get { return (IElementProvider)GetValue(DataProperty); }
                set { SetValue(DataProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty DataProperty =
                DependencyProperty.Register("Data", typeof(IElementProvider), typeof(UserControl1), new PropertyMetadata(new PropertyChangedCallback(SpecialChangedPropertyCallback)));
    
            private static void SpecialChangedPropertyCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                UserControl1 myUserControl = (UserControl1)d;
                // I want to assign the new Dependency Property to some other object here
            }


    srithar

    • Marked as answer by IamHuM Sunday, December 22, 2013 1:31 PM
    Wednesday, December 18, 2013 5:06 AM

All replies

  • The SpecialChangedPropertyCallback doesn't get called or what is you issue here? The dependency property seems to be implemented correctly but you need to make sure that the "Parameters" property that is being set as the UserControl's DataContext is of type IElementProvider since you define your dependency property to be of this type. It will probably work if you change the type of the dependency property from IElementProvider to object.
    Tuesday, December 17, 2013 3:44 PM
  • Ya Magnus, actually the SpecialChangedPropertyCallback  is not getting called in my code. My class which i binding is derived from 'IElementProvider'.

    So i have to use type as 'IElementProvider' in the Dependency Property.

    Not sure but the object which i am binding to is also derived from 'INotifyPropertyChanged' and another interface in my project. Can this be the issue here?

    Thanks,

    IamHuM

    Tuesday, December 17, 2013 6:27 PM
  • Hi,

    Since you are binding to the DataContext of the same usercontrol. set the Dependency property in the Code-Behind. You can pin point what is the issue.

          public UserControl1()
            {
                InitializeComponent();
                this.Loaded += UserControl1_Loaded;
            }
    
            void UserControl1_Loaded(object sender, RoutedEventArgs e)
            {
                Data = (IElementProvider)((sender as UserControl).DataContext);
            }
    
            public IElementProvider Data
            {
                get { return (IElementProvider)GetValue(DataProperty); }
                set { SetValue(DataProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty DataProperty =
                DependencyProperty.Register("Data", typeof(IElementProvider), typeof(UserControl1), new PropertyMetadata(new PropertyChangedCallback(SpecialChangedPropertyCallback)));
    
            private static void SpecialChangedPropertyCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                UserControl1 myUserControl = (UserControl1)d;
                // I want to assign the new Dependency Property to some other object here
            }


    srithar

    • Marked as answer by IamHuM Sunday, December 22, 2013 1:31 PM
    Wednesday, December 18, 2013 5:06 AM
  • Why are you binding Data to DataContext and then futhermore DataContext to Parameter? You could simply bind the Data to Parameters and skip all the troubles.

    Btw your binding fails because DataContext is simple object and not of type IElementProvider...


    • Edited by dev hedgehog Wednesday, December 18, 2013 6:26 AM
    Wednesday, December 18, 2013 6:26 AM
  • If the Parameters property of the UserControl's DataContext is of a type that implements the custom IElementProvider interface, you should be able to bind the property like this:

    <Views:UserControl1 x:Name="myUserControl1" 
                Data="{Binding Path=Parameters}" >
    

    Wednesday, December 18, 2013 6:29 PM