WPF:嵌套依赖属性
我在窗口上有一个“Layouts”的 ObservableCollection 和一个“SelectedLocation”DependencyProperty。 SelectedLocation 有一个名为“Layout”的属性,它是一个包含“Name”等字段的对象。我试图将组合框绑定到 SelectedLayout 但它不起作用。 以下不起作用,我尝试绑定到 SelectedItem 但无济于事。我相信这可能与我绑定到 SelectedLocation DependencyProperty 的子属性有关(尽管这确实实现了 INotifyPropertyChanged。
<ComboBox Grid.Row="2" Grid.Column="0" x:Name="cboLayout" ItemsSource="{Binding Layouts,ElementName=root}" SelectedValue="{Binding SelectedLocation.Layout.LayoutID,ElementName=root}" DisplayMemberPath="{Binding Name}" SelectedValuePath="LayoutID" />
但是,以下工作有效(也绑定到“SelectedLocation”DP:
<TextBox Grid.Row="4" Grid.Column="1" x:Name="txtName" Text="{Binding SelectedLocation.Name,ElementName=root,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
I have an ObservableCollection of "Layouts" and a "SelectedLocation" DependencyProperty on a Window. The SelectedLocation has a property called "Layout", which is an object containing fields like "Name" etc. I'm trying to bind a combobox to the SelectedLayout but it's not working.
The following does not work, I've tried binding to SelectedItem instead to no avail. I believe it may be something to do with the fact that I'm binding to a subProperty of the SelectedLocation DependencyProperty (though this does implement INotifyPropertyChanged.
<ComboBox Grid.Row="2" Grid.Column="0" x:Name="cboLayout" ItemsSource="{Binding Layouts,ElementName=root}" SelectedValue="{Binding SelectedLocation.Layout.LayoutID,ElementName=root}" DisplayMemberPath="{Binding Name}" SelectedValuePath="LayoutID" />
However, the following works (Also bound to the "SelectedLocation" DP:
<TextBox Grid.Row="4" Grid.Column="1" x:Name="txtName" Text="{Binding SelectedLocation.Name,ElementName=root,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Layouts
有什么类型的属性?我想是这样的:IEnumerable
。但是您将选定的值绑定到 Layout.LayoutID 。所以你遇到的情况是,当组合框包含
Layout
对象时,你尝试通过Int
标识符选择它。当然,绑定引擎在那里找不到任何Int
。我不知道您的代码的详细信息,所以我可以建议一件事:尝试减少您的绑定表达式:
SelectedItem="{Binding SelectedLocation.Layout,ElementName=root}
。如果没有成功,请提供更多代码来帮助我理解发生了什么。
====更新====
正如我所说,你显然做错了什么,但我不是超自然主义者,无法猜测。你失败的原因(没有你的代码)。如果您不想共享您的代码,我决定提供简单的示例来证明一切正常。请查看下面所示的代码,并告诉我您的应用程序中有什么不同
。公开属性 LayoutId 的布局:
具有嵌套属性 Layout 的 Class SelectionLocation:
以及具有依赖属性的 Window 类(实际上,在我的示例中 StartupView 是 UserControl,但这并不重要):
StartupView 的 XAML:
What type property
Layouts
has? I suppose something like this this:IEnumerable<Layout>
.But you bind selected value to
Layout.LayoutID
. So you got situation, when combo box containsLayout
objects, and you try to select it byInt
identifier. Of course binding engine can't find anyInt
there.I have no idea about details of your code, so one thing I could propose: try to reduce your binding expression:
SelectedItem="{Binding SelectedLocation.Layout,ElementName=root}
.If no success, provide more code to help me understand what's going on.
====UPDATE====
As I've said, you are obviously doing something wrong. But I am not paranormalist and couldn't guess the reason of your fail (without your code). If you don't want to share your code, I decided to provide simple example in order to demonstrate that everything works. Have a look at code shown below and tell me what is different in your application.
Class Layout which exposes property LayoutId:
Class SelectionLocation which has nested property Layout:
And Window class with dependency properties (actually, in my example StartupView is UserControl, but it doesn't matter):
XAML of StartupView: