tags:

views:

631

answers:

2

I have a diagram designer program and I want to add texts to shapes (these are path objects) when user right clicks to a shape and writes shape name in the property window. I add a context menu property to shapes and when user clicks to "properties" in the context menu, a new window opens that has a textbox and a button. I can add a textblock to the shapes but I cannot bind the textbox in the property window to the textblock in the shape. What I want is when user enters a text into textbox in the property window and clicks OK button, the textblock on the shape changes to the text that user entered.

Thanks.

A: 

You can bind one control to another using an ElementName binding:

<TextBlock Text="{Binding Text, ElementName=TextBoxInPropertiesWindow}" />

but that's probably not what you want in this case, because it sounds like the properties window and the text box will soon be going away and/or being reused to edit other diagram elements.

You therefore really need to be thinking in terms of binding both the text box and the text block to the underlying data model / viewmodel. In this way, the text box can update the model (which will still remain after the text box is destroyed), and the text block will then update in response to the change in the model.

itowlson
First, thanks for your concern. But I do not know how to do "binding both the text box and the text block to the underlying data model / viewmodel". Can you explain it please?
You have some sort of underlying data model of the diagram, say a Diagram object with a collection of Shape objects. Each TextBlock on the UI is bound to a Shape.Text. Now provide a SelectedShape property on the Diagram (or in the view model if that is not appropriate), update that as the selection changes, and bind the TextBox to SelectedShape.Text. Hope this makes more sense now!
itowlson
I've done it. It takes a little bit time but finally it worked. Thanks for your help again.
A: 

Bind the selected shape's datacontext to a property on the window or controller called SelectedItem, then bind the property window's datacontext to SelectedItem.

NotDan