WPF 跨元素树分支路由事件
我想知道在 WPF 中启用控件之间通信的正确机制是什么。 我的目标是不使用常规事件,而必须手动连接它们。 路由命令(隧道、冒泡)的默认行为似乎是正确的,但我想我错过了一些东西。
路由事件是 WPF 提供的新基础设施,它允许事件沿着可视树向下传输到目标元素,或者向上冒泡到根元素。 当事件被引发时,它会在可视化树中向上或向下“移动”,在途中遇到的订阅该事件的任何元素上调用该事件的处理程序。 注意,这个树遍历并没有覆盖整个视觉树,只覆盖祖先元素
即来自此WPF 文章
使用文章中的图像,我希望“立即元素 #1”启动(引发)一个事件,然后让“立即元素#2”处理该事件。 我想实现这一点,而不必在“根元素”中放置任何代码。
基本上从我的应用程序中的任何位置触发一个事件(保存、状态更新、选择更改等......),然后在其他地方处理它,而两方彼此不了解任何信息。 这可能吗?
我不认为数据绑定是答案。 我想使用路由事件/命令,因为它们是在整个树上设计的,而不仅仅是在源代码管理的分支内。 也许无法使用路由事件/命令来完成,而数据绑定就是答案。 我只是不知道...
有什么想法吗?
I am wondering what the correct mechanism to enable communication between controls in WPF is. My goal is to not use conventional events and have to manually wire them up. The default behavior of routed commands (tunneling, bubbling) seems to be along the right lines but I guess I'm missing something.
Routed events are a new infrastructure provided by WPF which allows events to tunnel down the visual tree to the target element, or bubble up to the root element. When an event is raised, it “travels” up or down the visual tree invoking handlers for that event on any element subscribed to that event it encounters en route. Note that this tree traversal does not cover the entire visual tree, only the ancestral element
That is from this WPF Article
Using the image in the article, I want "Immediate Element #1" to initiate (raise) an event and then have "Immediate Element #2" handle that event. I'd like to achieve this without having to put any code in the "Root Element".
Basically fire an event (save, status updated, selection changed, etc..) from any where in my app, then have it be handled somewhere else with out the 2 parties knowing anything about each other. Is this possible?
I dont believe data bainding is the answer. I'd like to use Routed Events / Commands as they were designed just across the entire tree, not just within the source control's branch. Maybe it can't be done using routed events / commands, and data binding is the answer. I just dont know...
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最好的机制是重构数据视图并将其与数据模型分离。
创建一个为每个数据点提供 DependencyProperty 属性(而不是标准 C# 属性)的数据模型,但不提供 UI。 数据模型中的值在修改时可能会相互影响。
然后,您可以将每个 WPF 元素绑定到数据模型中适当的 DependencyProperty。 修改一个元素中的值,所有其他元素都会更新,以反映绑定属性中的任何数据模型更改。
The best mechanism is to refactor and separate the data view from the data model.
Create a data model that provides DependencyProperty properties (rather than standard C# properties) for each data point, but does not provide a UI. The values in the data model can affect each other when modified.
You can then bind each WPF element to the appropriate DependencyProperty from the data model. Modify the value in one element and all other elements are updated to reflect any data model changes in the bound properties.
如果您想在元素之间传输数据,
Binding
是最佳选择。 网上有很多关于这方面的教程和书籍。如果您想实现
Style
更改,则可以使用DataTriggers
,它也使用 Bindings。如果不将其连接到公共根,则无法在不相关的控件之间发送传统意义上的事件。
If you want to transfer data between elements,
Binding
is the way to go. There are many tutorials and books about this on the net.If you want to effect
Style
changes, then you can useDataTriggers
, which also use Bindings.There is no way to send events in the traditional sense between unrelated controls without wiring it up in the common root.