使用控件模板项属性同步templatedParent属性

时间:2013-07-02 09:44:08

标签: c# wpf xaml data-binding

使用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}

但它不起作用。

0 个答案:

没有答案
相关问题