我同时安装了 VS2008 和 VS2010,我看到一个非常奇怪的行为
在 VS2008 中,我有一个简单的 WPF 应用程序:
<TextBox x:Name="textbox" Text="{Binding Path=MyProperty,Mode=TwoWay}"></TextBox>
和
public Window1()
{
InitializeComponent();
DataContext = this;
}
public string MyProperty
{
get { return (string)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(string), typeof(Window1), new PropertyMetadata("default",null,Coerce));
private static object Coerce(DependencyObject d, object baseValue)
{
return "Coerced Value";
}
当我在文本框中输入随机字符串并点击选项卡时,我希望 textbox.Text 被重置为“强制值”。如果我调试我看到应用程序在强制功能中中断,但 UI 没有更新。
有趣的是,同样的代码在 VS2010 中也可以使用,UI 会使用强制值进行更新。有人知道发生了什么吗?
这是一个WPF错误吗?还是我错过了什么?