WPF控件的嵌套属性的数据绑定 [英] WPF Control's Nested property's data binding

查看:227
本文介绍了WPF控件的嵌套属性的数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一些嵌套的属性,允许使用数据绑定来设置用户的控制。例如,我有这样的事情:

  //顶级控
公共类MyControl:控制
{
公共字符串TopLevelTestProperty
{
{返回(串)的GetValue(TopLevelTestPropertyProperty); }
集合{的SetValue(TopLevelTestPropertyProperty,值); }
}

公共静态只读的DependencyProperty TopLevelTestPropertyProperty =
DependencyProperty.Register(TopLevelTestProperty的typeof(串)的typeof
(MyControl),新UIPropertyMetadata( ));

//此属性包含嵌套的对象
公共MyNestedType NestedObject
{
{返回(MyNestedType)的GetValue(NestedObjectProperty); }
集合{的SetValue(NestedObjectProperty,值); }
}

公共静态只读的DependencyProperty NestedObjectProperty =
DependencyProperty.Register(NestedObject的typeof(MyNestedType)的typeof
(MyControl),新UIPropertyMetadata(空));
}

//嵌套对象的类型
公共类MyNestedType:DependencyObject的
{
公共字符串NestedTestProperty
{
获得{返回(串)的GetValue(NestedTestPropertyProperty); }
集合{的SetValue(NestedTestPropertyProperty,值); }
}

公共静态只读的DependencyProperty NestedTestPropertyProperty =
DependencyProperty.Register(NestedTestProperty的typeof(串)的typeof
(MyNestedType),新UIPropertyMetadata( ));
}

//数据采样范围内
公共类TestDataContext
{
公共字符串值
{
得到
{
返回测试值!
}
}
}
...
this.DataContext =新TestDataContext();
...



XAML:

 <局部:mycontrol X:名称=显示myControltopleveltestproperty ={绑定值}> 
<局部:mycontrol.nestedobject>
<局部:mynestedtype X:NAME =myNestedControlnestedtestproperty ={绑定值}/>
< /地方:mycontrol.nestedobject>
< /地方:mycontrol>



它非常适用属性TopLevelTestProperty,但它不为NestedTestProperty工作。
似乎嵌套绑定不起作用。任何人可以帮助我吗?有没有什么办法让这样的结合?
我认为,这是因为我的嵌套对象有没有到顶级对象的任何引用,所以它不能使用MyControl的DataContext的得到解决。


解决方案

HB 权,嵌套控制不继承的DataContext mycontrol
酪氨酸出设置它明确:



<预类=郎咸平的XML prettyprint-覆盖> <局部:mycontrol X:名称=显示myControl
topleveltestproperty ={绑定值}>
<局部:mycontrol.nestedobject>
<局部:mynestedtype X:NAME =myNestedControl
的DataContext ={绑定的ElementName = myControl,
路径=的DataContext}
nestedtestproperty ={绑定值} />
< /地方:mycontrol.nestedobject>
< /地方:mycontrol>


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:

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

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.

解决方案

H.B. right, nested control does not inherit DataContext from mycontrol. Tyr out setting it explicitly:

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

这篇关于WPF控件的嵌套属性的数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆