我有一个TextBox
,我正在尝试将它绑定到一个DependencyProperty
. 加载时或我输入TextBox
. 我错过了什么?
XAML
<UserControl:Class="TestBinding.UsernameBox"
// removed xmlns stuff here for clarity>
<Grid>
<TextBox Height="23" Name="usernameTextBox" Text="{Binding Path=Username, ElementName=myWindow, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
</Grid>
</UserControl>
C#
public partial class UsernameBox : UserControl
{
public UsernameBox()
{
InitializeComponent();
}
public string Username
{
get
{
// Control never reaches here
return (string)GetValue(UsernameProperty);
}
set
{
// Control never reaches here
SetValue(UsernameProperty, value);
}
}
public static readonly DependencyProperty UsernameProperty
= DependencyProperty.Register("Username", typeof(string), typeof(MainWindow));
}
编辑:我需要实现 aDependencyProperty
因为我正在创建自己的控件。