Hopefully a simple starter question from a newbie…
I have a TextBox whose Text property is bound to a ViewModel and DependencyProperty.
When I click on the TextBox I want a second TextBox (an ‘Editor’ TextBox) to be assigned the same binding as the first. The outcome being that editing the second ‘Editor’ TextBox will update the first.
Ultimately I want to be able to click any TextBox and edit it in the same ‘Editor’ TextBox.
My Solution using Option 2… Thanks!!:
private void m_sourceTextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
TextBox sourceTextBox = sender as TextBox;
if (null != sourceTextBox)
{
BindingExpression sourceBindExpression = sourceTextBox.GetBindingExpression(TextBox.TextProperty);
if (sourceBindExpression != null && sourceBindExpression.ParentBinding != null && sourceBindExpression.ParentBinding.Path != null)
m_editorTextBox.SetBinding(TextBox.TextProperty, sourceBindExpression.ParentBinding);
}
}
I can think of two ways of doing this
The first, is to have a
SelectedTextproperty in your ViewModel that yourEditorTextBoxis bound to, and set this value when you click on any of the otherTextBoxes. For this to work, you’ll probably need something like an AttachedCommandBehavior so you can attach a Command from the ViewModel to theClickorFocusevent of the TextBox.The other way I can think of doing it is just to do it in code-behind. In the
ClickorFocusevent of each TextBox, get theBindingExpressionfor the selected TextBox’sTextProperty, and copy the binding to theEditorTextBox.Text.