WPF:如何绑定到嵌套属性?

编程入门 行业动态 更新时间:2024-10-22 19:22:39
本文介绍了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:如何绑定到嵌套属性?

本文发布于:2023-11-12 12:30:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581502.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   绑定   属性   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!