我正在创建一个自定义控件,其中包含PasswordBox
。如何将我的自定义控件的DependencyProperty
连接到PasswordBox
的Password
属性?WP7自定义控件绑定内部控件
从我看到的所有示例中,使用TemplateBinding
将模板中的密码挂钩到模板中应该可以做到,但这似乎不起作用。我错过了什么?
generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:CustomControlBinding="clr-namespace:CustomControlBinding">
<Style TargetType="CustomControlBinding:PasswordBoxTest">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="CustomControlBinding:PasswordBoxTest">
<Grid Background="Transparent">
<PasswordBox Password="{TemplateBinding Text}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
PasswordBoxTest.cs
namespace CustomControlBinding
{
public class PasswordBoxTest : Control
{
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(PasswordBoxTest), new PropertyMetadata(OnTextPropertyChanged));
public string Text
{
get { return GetValue(TextProperty) as string; }
set { SetValue(TextProperty, value); }
}
public PasswordBoxTest()
{
DefaultStyleKey = typeof(PasswordBoxTest);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
}
private static void OnTextPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
}
}
}
是不是使用与TemplateBinding相同的RelativeSource? '{Binding RelativeSource = {RelativeSource TemplatedParent}}'http://msdn.microsoft.com/en-us/library/ms742882.aspx –