WPF:如何绑定到嵌套属性? [英] WPF: How to bind to a nested property?

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

问题描述

我可以绑定到一个属性,但其他属性中没有的属性。为什么不?例如。

 <窗​​口的DataContext ={绑定的RelativeSource = {自我的RelativeSource}}...>
...
    <! - 不工作 - >
    <文本框的文本={绑定路径= ParentProperty.ChildProperty,模式=双向}
             宽度=30/>

(注意:我并不想这样做主人的细节或任何这两个属性是标准的CLR性能。)

更新:问题是,我ParentProperty依赖XAML中的对象进行初始化。不幸的是该对象在低于绑定XAML文件后面定义,所以对象为空时,在我的ParentProperty被绑定读取时间。由于重新排列XAML文件会搞砸了布局,我能想到的唯一的解决办法是定义绑定code-背后:

 <文本框X:NAME =TXTWIDTH =30/>//调用后在InitializeComponent()
txt.SetBinding(TextBox.TextPropertyParentProperty.ChildProperty);


解决方案

所有我能想到的是, ParentProperty 后正在发生变化的结合创建,它不支持更改通知。链中的每个属性都必须支持更改通知,无论是由于是一个的DependencyProperty ,或通过实施 INotifyPropertyChanged的

I can bind to a property, but not a property within another property. Why not? e.g.

<Window DataContext="{Binding RelativeSource={RelativeSource Self}}"...>
...
    <!--Doesn't work-->
    <TextBox Text="{Binding Path=ParentProperty.ChildProperty,Mode=TwoWay}" 
             Width="30"/>

(Note: I'm not trying to do master-details or anything. Both properties are standard CLR properties.)

Update: the problem was that my ParentProperty depended on an object in XAML being initialized. Unfortunately that object was defined later in the XAML file than the Binding, so the object was null at the time when my ParentProperty was read by the Binding. Since rearranging the XAML file would screw up the layout, the only solution I could think of was to define the Binding in code-behind:

<TextBox x:Name="txt" Width="30"/>

// after calling InitializeComponent()
txt.SetBinding(TextBox.TextProperty, "ParentProperty.ChildProperty");

解决方案

All I can think of is that the ParentProperty is being changed after the Binding is created, and it does not support change notification. Every property in the chain must support change notification, whether it be by virtue of being a DependencyProperty, or by implementing INotifyPropertyChanged.

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

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