0

使用 WPF 我正在尝试将 TextBox 的 Text 属性绑定到自定义控件的 Text 属性。谁能告诉我如何实现这一目标?

首先,我有包含 TextBox 的控件模板:

<TextBox x:Name="PART_InputTextBox" 
    Text="[???]">
</TextBox>

我使用此模板的自定义控件包含 DependencyProperty "Text"

public static readonly DependencyProperty TextProperty =
   DependencyProperty.Register("Text", typeof(string), typeof(AutocompleteSelector), new UIPropertyMetadata(null));
public string Text
{
    get
    {
        return (string)GetValue(TextProperty);
    }
    set
    {
        SetValue(TextProperty, value);
    }
}

我的问题是:我应该在 TextBox 的 Text 属性中使用什么绑定(而不是 [???])来获得这些属性的双向同步?我的意思是,当 CustomControl.Text 将被更改时,我也想更改 TextBox.Text,反之亦然。我已经试过了

{Binding Text, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}

但它不起作用。

4

0 回答 0