问题描述
我看着高低不一,在这里砸了头太久了.使用MVVM,Caliburn Micro,WPF,XAML,并尝试将数据后期绑定到节点.我已经在第一层或第二层进行了数据绑定,并且想要监视节点上的扩展"事件,因此我可以检查我的小延迟读取子节点,也许可以读取数据并生长树.我已经尝试了很多事情,我现在已经确定我应该可以做到.
I have looked high and low and smashed head here too long. Working with MVVM, Caliburn Micro, WPF, XAML, and trying to late bind the data to the nodes. I have databound the first level or two, and am wanting to watch for "Expanded" events on the nodes so I could check for my little late fetch child node and perhaps do fetch data and grow the tree. I have tried many things, I think I am set now that I should be able to essentially do..
Message.Attach="[Event TreeViewItem.Expanded] = [Action NodeExpanding($this)]"
但这永远不会触发...在同一级别上,以下工作正常:
But that just doesn't ever fire... At the same level the following works just fine:
Message.Attach="[Event SelectedItemChanged] = [Action SetSelectedItem($this.SelectedItem)]"
并且我确实使该事件在ViewModel上正常启动,因此我必须关闭...
and I do get this event to fire well on my ViewModel so I must be close...
任何帮助表示赞赏!tnx,-J
Any help appreciated! tnx, -J
推荐答案
我的猜测(可能很接近)是它可能与节点的 DataContext
有关.CM默认使用 DataContext
作为操作的目标,因此,在没有显式设置上下文的情况下,您尝试对绑定到该节点的数据项(当然还有该数据项)触发事件没有任何办法可以解决这个问题.
My guess (and it's probably close) is that it's likely to do with the DataContext
of the nodes. CM uses the DataContext
as the target for actions by default, so without explicity setting a context, you are attempting to fire an event against the data item which is bound to the node, and of course the data item doesn't have any way to handle this.
您仍然可以通过在树视图项上设置 Action.TargetWithoutContext
附加属性来为操作设置目标-通过这种方式,CM知道在何处查找消息
You can still set a target for the action by setting the Action.TargetWithoutContext
attached property on the treeview item - this way CM knows where to look to wire up the message
通常,我发现与祖先的绑定最简单:
Usually I find binding to an ancestor is easiest:
<TreeViewItem cal:Action.TargetWithoutContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType=UserControl}}" />
或者您可以使用 ElementName
,不管您做什么,只要记住您需要指向目标ViewModel,它将成为父容器的 DataContext
.我已经忘记了几次使用 DataContext
,并且对为什么它不起作用ed之以鼻(由于绑定仍然有效,但没有绑定错误,所以您没有绑定错误-CMLogManager.GetLog()在这里有帮助!)
Or you can use ElementName
, whatever works for you, just remember you need to point to the target ViewModel which will be the DataContext
of the parent container. I've forgotten to use DataContext
on several occasions and scratched my head as to why it didn't work (you get no binding errors because your binding is still valid but it just doesn't work - CM LogManager.GetLog() helps here!)
这篇关于WPF Caliburn Micro TreeView HierarchicalDataTemplate节点扩展事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!