简体   繁体   中英

WPF Adding textblock on an item and binding it to a textbox

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.

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.

将选定形状的数据上下文绑定到窗口或名为SelectedItem的控制器上的属性,然后将属性窗口的数据上下文绑定到SelectedItem。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM