【发布时间】:2012-12-06 08:12:45
【问题描述】:
我有一个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));
}
编辑:我需要实现DependencyProperty,因为我正在创建自己的控件。
【问题讨论】:
-
您不应该使用 DependencyProperty 作为绑定源!看我的回答...
-
糟糕,我没有说清楚。我实际上是在创建自己的控件,所以它需要是一个依赖属性。
标签: wpf binding textbox dependency-properties