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

[亡魂溺海] 提交于 2019-12-02 07:22:00

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/775146/wpf-adding-textblock-on-an-item-and-binding-it-to-a-textbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!