WPF控件的嵌套属性数据绑定

4

我正在尝试开发一个带有嵌套属性的用户控件,允许使用数据绑定进行设置。例如,我有这样的东西:

// 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:

      <local:mycontrol x:name="myControl" topleveltestproperty="{Binding Value}" >
         <local:mycontrol.nestedobject>
            <local:mynestedtype x:name="myNestedControl" nestedtestproperty="{Binding Value}" />
         </local:mycontrol.nestedobject>
      </local:mycontrol>

它对于TopLevelTestProperty属性工作得很好,但对于NestedTestProperty则不起作用。 似乎嵌套绑定不起作用。有人能帮我吗?是否有任何方法可以进行这种绑定? 我认为这是因为我的嵌套对象没有任何参考顶层对象,因此无法使用MyControl的DataContext解决。


绑定错误提示了什么? - H.B.
它说:“无法找到目标元素的控制 FrameworkElement 或 FrameworkContentElement。BindingExpression: Path=Value; DataItem=null; 目标元素为 'MyNestedType',目标属性为 'NestedTestProperty'(类型为 'String')”。 - D.P.
1
你缺少继承上下文 - H.B.
那看起来不错...我想知道是否可以将其添加到我的类型中... - D.P.
1个回答

0

H.B. 没错,嵌套控件不会从 mycontrol 继承 DataContext。尝试显式设置它:

<local:mycontrol x:name="myControl" 
                 topleveltestproperty="{Binding Value}" >          
   <local:mycontrol.nestedobject>             
           <local:mynestedtype x:name="myNestedControl" 
                               DataContext="{Binding ElementName=myControl,
                                                     Path=DataContext}"
                               nestedtestproperty="{Binding Value}" />          
  </local:mycontrol.nestedobject>       
</local:mycontrol> 

那么唯一的方法就是从FrameworkElement或FrameworkContentElement继承mynestedtype(因为mynestedtype中没有DataContext属性),是吗?但是是否有任何方法可以对DependencyObject进行继承呢?FrameworkElement提供了很多我不想要的属性。 - D.P.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接