Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I'm trying to develop user control with some nested properties that allows to use databinding to set it. For example, I have something like this:
C#
// Top level control
    public class MyControl : Control
    {
        public string TopLevelTestProperty
        {
            get { return (string)GetValue(TopLevelTestPropertyProperty); }
            set { SetValue(TopLevelTestPropertyProperty, value); }
        }

        public static readonly DependencyProperty TopLevelTestPropertyProperty =
            DependencyProperty.Register("TopLevelTestProperty", typeof(string), typeof   
               (MyControl), new UIPropertyMetadata(""));

        // This property contains nested object
        public MyNestedType NestedObject
        {
            get { return (MyNestedType)GetValue(NestedObjectProperty); }
            set { SetValue(NestedObjectProperty, value); }
        }

        public static readonly DependencyProperty NestedObjectProperty =
            DependencyProperty.Register("NestedObject", typeof(MyNestedType), typeof 
                (MyControl), new UIPropertyMetadata(null));
    }

    // Nested object's type
    public class MyNestedType : DependencyObject
    {
        public string NestedTestProperty
        {
            get { return (string)GetValue(NestedTestPropertyProperty); }
            set { SetValue(NestedTestPropertyProperty, value); }
        }

        public static readonly DependencyProperty NestedTestPropertyProperty =
            DependencyProperty.Register("NestedTestProperty", typeof(string), typeof
                (MyNestedType), new UIPropertyMetadata(""));
    }

    // Sample data context
    public class TestDataContext
    {
        public string Value
        {
            get
            {
                return "TEST VALUE!!!";
            }
        }
    }

   ...
   this.DataContext = new TestDataContext();
   ...

XAML:
HTML
<local:mycontrol x:name="myControl" topleveltestproperty="{Binding Value}" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:MyApplication">
   <local:mycontrol.nestedobject>
      <local:mynestedtype x:name="myNestedControl" nestedtestproperty="{Binding Value}" />
   </local:mycontrol.nestedobject>
</local:mycontrol>


It works well for property TopLevelTestProperty, but it doesn't work for NestedTestProperty.
It seems that nested bindings do not work. Can anybody help me please? Is there any way to make such binding?
I think that it happens because of my nested object has no any reference to the top level object, so it cannot be resolved using MyControl's DataContext.
Posted
Updated 22-Nov-11 23:19pm
v2
Comments
Mark Salsbery 23-Nov-11 11:14am    
Does this work? NestedObject.NestedTestProperty="{Binding Value}"
DmitryPl 23-Nov-11 21:30pm    
Did you mean something like this:
<local:mycontrol nestedobject.nestedtestproperty="{Binding Value}">?
It doesn't work, VS error:
The attachable property 'NestedTestProperty' was not found in type 'NestedObject'.
Mark Salsbery 24-Nov-11 13:36pm    
Doesn't seem to be supported in XAML. See posts here for alternatives... XAML - Setting value on nested property ?[^]
DmitryPl 27-Nov-11 21:33pm    
Thanks Mark, it seems that there is no simple way to do that for nested properties of DependencyObject type. I can change MyNestedType's base type to FrameworkElement and set it's DataContext manually (as it will not be inherited in this case), but it looks like workaround, not proper solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900