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