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

后端 未结 2 1895
灰色年华
灰色年华 2021-01-23 11:41

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 ad

相关标签:
2条回答
  • 2021-01-23 12:00

    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.

    0 讨论(0)
  • 2021-01-23 12:01

    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.

    0 讨论(0)
提交回复
热议问题