[英]WPF, Data Binding, Lookup hierarchy
I am new to WPF, and trying to develop a small application using DataBinding following the MVVM paradigm. 我是WPF的新手,并尝试按照MVVM范例使用DataBinding开发一个小型应用程序。
I am trying to provide a binding functionality to the ObservableCollection of Documents (my type). 我正在尝试为文档的ObservableCollection(我的类型)提供绑定功能。 The First Label would display a Documents path (available through Attribute property), while the next children - Pages of the Documents - would display appropriate information: page index and page content (an image). 第一个标签将显示“文档”路径(可通过Attribute属性获得),而下一个子级(“文档”页面)将显示适当的信息:页面索引和页面内容(图像)。
Here is an issue - how can I create a lookup binding to a parent Label? 这是一个问题-如何创建与父Label的查找绑定? On Button Click command I would like to pass the documents path, which was available earlier in the first DataTemplate. “单击按钮”命令我想传递文档路径,该路径在前面的第一个DataTemplate中可用。
Is there a way to tackle this issue? 有没有办法解决这个问题? How would you recommend going around it? 您会如何建议解决?
Also, is there a better way to tackle "nested" structures (collections within collections)? 此外,是否有更好的方法来处理“嵌套”结构(集合中的集合)?
Here is the code https://gist.github.com/b5760982ba81e8ee4036 , line 14 这是代码https://gist.github.com/b5760982ba81e8ee4036 ,第14行
you should use RelativeSource: 您应该使用RelativeSource:
<ItemsControl ItemsSource="{Binding Documents}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Label Content="{Binding Path=Attribute.Path}"/>
<ItemsControl ItemsSource="{Binding Pages}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Label Content="{Binding Index}"/>
<Button Content="{Binding Content}"
Command="{x:Static viewModel:DocViewModel.Tests }"
CommandParameter="{Binding Path=DataContext.Attribute.Path,RelativeSource={RelativeSource AncestorType=ContentPresenter, Mode=FindAncestor,AncestorLevel=2"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.