我有一个用户控件我创建了一个名为TextBoxAndMore,它包含一个文本框(称为textBox1),旁边有一个“更多”按钮。在这个用户控件上,我有一个名为Text的属性。我想要这个Text属性来镜像文本框中的文本。在我的用户控件绑定不起作用
我想这个属性是可绑定,这样在我的XAML,我可以把它绑定到名为说明在我的ViewModel一个String属性,像这样:
<my:TextBoxAndMore Text="{Binding Path=Description}" />
所以我下面的代码(由propdb片段生成):
// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(String), typeof(TextBoxAndMore),
new UIPropertyMetadata(String.Empty));
public String Text
{
get { return (String)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
而且,在文本框中,我有连接到TextChanged事件处理程序,其想法是这样的代码,当文本框中的用户类型,该用户的Text属性控制随之变化:
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
Text = textBox1.Text;
}
但是,最终结果是绑定根本不起作用。在输出窗口中没有错误,并且我的ViewModel中的Description属性没有被设置(它仍然为空)。
我确定这是显而易见的,但我对WPF很陌生,并希望得到一些指导。
相反,你想改变绑定为“TwoWay”,以便用户进行的更新自动返回到属性。 – Gabe 2011-02-07 17:00:08
谢谢,我已经将XAML更改为` `(这是你的意思吗?),但我似乎仍然有同样的问题。 –
Matt
2011-02-07 17:04:54