3

希望来自新手的一个简单的入门问题......

我有一个 TextBox,其 Text 属性绑定到 ViewModel 和 DependencyProperty。

当我单击文本框时,我希望为第二个文本框(“编辑器”文本框)分配与第一个相同的绑定。结果是编辑第二个“编辑器”文本框将更新第一个。

最终,我希望能够单击任何文本框并在同一个“编辑器”文本框中进行编辑。


我使用选项 2 的解决方案...谢谢!!:

    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);
        }
    }
4

1 回答 1

2

我可以想到两种方法

第一个是SelectedText在您的 ViewModel 中拥有一个EditorTextBox绑定到的属性,并在您单击其他任何一个时设置此值TextBoxes。为此,您可能需要AttachedCommandBehavior之类的东西,这样您就可以将 ViewModel 中的命令附加到 TextBox 的ClickFocus事件。

我能想到的另一种方法就是在代码隐藏中进行。在每个 TextBox 的ClickorFocus事件中,获取BindingExpression所选 TextBox 的TextProperty,并将绑定复制到EditorTextBox.Text.

于 2012-10-19T12:26:07.270 回答