WPF 事件、命令或同时使用两者
我正在构建我的应用程序基础结构,并发现很难实现非常基本的行为 - 我想从系统中的不同用户控件引发事件,并能够在侦听它们的其他一些用户控件上捕获这些事件。例如,我有一个实现 TreeView 的用户控件。我有另一个实现 ListView 的用户控件。现在,我希望我的 ListView 能够监听 TreeView,并且当 TreeView 上的选择发生更改时,我想相应地重新填充我的 ListView。 即使 ListView 不在 WPF 逻辑树上的 TreeView 内,我也希望发生这种情况。
请帮忙!
谢谢, 奥兰
I am constructing my app infra structure, and finding it hard to achieve a very basic behavior - I want to raise events from different user controls in the system and being able to catch those events on some other user controls that listens to them. For example i have a user control that implements a TreeView. I have another user control that implmements a ListView. Now, i want my ListView to listen to the TreeView, and when the selection is changed on the TreeView, i want to repopulate my ListView accordingly.
I also want this to happen even if the ListView is not located within the TreeView on the WPF logical tree.
PLEASE HELP!!
Thanks,
Oran
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用数据绑定。
如果列表视图的内容存储在树视图中显示的对象内,则只需绑定到树 SelectedItem 属性即可。
否则,将树 SelectedItem 绑定到视图模型(或窗口!)中的属性,并在此属性的设置器中更改绑定到列表视图 ItemSource 属性的列表。
您可以在 我的博客上的这个系列 我链接到的帖子是最后一篇带有代码下载链接的帖子,如果您想要完整的解释。
编辑:这是我在一个项目中的做法:(删除了 GridView 定义,因为它与此处无关)
绑定到树视图的 ItemsSource 中的列表是具有 3 个属性的对象:名称(即绑定(FolderTemplate 中的 TextBlock)、子文件夹(同样绑定到 HierarchicalDataTemplate.ItemsSource 属性)以及使用
{Binding ElementName=FolderTree, Path=SelectedItem.Files}
绑定到 ListView 的文件没有一个列表是可观察的集合(因为在这个项目中它们永远不会改变),而是由属性 getters 延迟加载(按需)(因为在这个项目中它们的加载成本很高)。
Use data binding.
If the content of the list view is stored inside the object shown in the tree view you can just bind into the tree SelectedItem property.
Otherwise bind the tree SelectedItem to a property in your view models (or your window!) and in the setter of this property change the list that is bound to the list view ItemSource property.
You can see the technique in this series on my blog the post I linked to is the last post with the code download link, you'll need to read from the beginning of the series if you want the full explanation.
EDIT: Here's how I did it in one project: (the GridView definition removed since it's not relevant here)
The list bound into the tree view's ItemsSource is of objects that have 3 properties: Name (that is bound to a TextBlock in the FolderTemplate), SubFolders (that is likewise bound to the HierarchicalDataTemplate.ItemsSource property) and Files that is bound to the ListView using
{Binding ElementName=FolderTree, Path=SelectedItem.Files}
Note that non of the lists are observable collections (because in this project they never change) but are loaded lazily (on-demand) by the properties getters (because in this project they are expensive to load).
此时,MVVM(模型-视图-视图模型模式)增加的复杂性可以开始得到回报。您需要的是发布/订阅基础设施,MVVM Light 具备这一点,以及良好的 MVVM 结构,不要变得过于复杂。 Prism 是另一个优秀的 WPF/Silverlight 基础设施基础,具有发布和订阅支持。
This is the point where the added complexity of MVVM (Model-View-ViewModel pattern) can start to pay off. What you need is a publish/subscribe infrastructure, and MVVM Light has that, along with good MVVM structure that doesn't get overly complex. Prism is another good WPF/Silverlight infrastructure foundation with publish and subscribe support.