SelectedNode Binding Does Not Work

I’ve try to data bind SelectedNode property of the Diagram to my ViewModel property but it is not updating it.

I’ve use SelectionChanged event but it is a workaround, not a solution.

Thank you,

This is odd, because when I try adding this binding to the ListBox in the EntityRelationship sample:

SelectedItem="{Binding ElementName=myDiagram, Path=SelectedNode.Data}"

and when I remove the SelectionChanged event handlers, this works the first time, but not for following selection changes.

I’m not sure why that is, so I do suggest you continue using the SelectionChanged event for now.

I can’t explain Silverlight’s behavior here.

Try adding this element to your app:

I find that the text shown always updates as expected.
So clearly the data-binding to Diagram.SelectedNode.Data is working.

But when I tried it before, data-binding ListBox.SelectedItem to Diagram.SelectedNode.Data, that only worked the first time.

Offhand I would say that’s a bug in Silverlight, either in ListBox or in data-binding, but it’s hard to tell.

Silverlight is not important to me, only WPF.

The same thing happens in WPF. Try that binding of TextBlock.Text to Diagram.SelectedNode.Data.

I have the same problem. I try to bind Diagram.SelectedNode to MyViewModel.SelectedNode but the bind doesn’t work. However the bind works fine when bound to TextBlock.Text. How did you fixed your problem ido.ran??

Do I miss something?

Are you trying to change the diagram’s primary selected Node based on some model data? Remember that Diagram.SelectedNode is a Node (i.e. a FrameworkElement). Also, since the Diagram.SelectedNode property is set by various code in the Diagram, if you do data-bind that property it ought to be a TwoWay binding.

I want to capture MouseDoubleClicked node in a ViewModel for then opening the info of that node in a dialog.

My first thought was to memorize the SelectedNode node in the ViewModel but, like you said, it forces me to couple the ViewModel to GoXam.Node class which is not a good idea.

Is there another way to accomplish what I trying to do?

I do not understand exactly what you want to do. What does double-clicking have to do with selection? Normally there can be multiple parts selected at once, which probably is not true for your details dialog.

It seems to me that your double-click event handler would set some view-model property indicating that that Node.Data should be the object for which details are shown.

Ok I found how! I created a ViewModel for each type of node that I have, and call the command directly from the DataTemplate, like this:


                <go:NodeShape Width="50" Height="50" 
                                  Stroke="Black" StrokeThickness="2" Fill="LightSlateGray" ToolTip="{Binding Data.Id}" >