我正在創建一個自定義控件,其中包含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 –