2

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)]" 

and I do get this event to fire well on my ViewModel so I must be close...

Any help appreciated! tnx, -J

Derek Beattie
  • 9,429
  • 4
  • 30
  • 44
James
  • 81
  • 6

1 Answers1

0

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.

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}}" />

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!)

Steven
  • 166,672
  • 24
  • 332
  • 435
Charleh
  • 13,749
  • 3
  • 37
  • 57