我同时安装了 VS2008 和 VS2010,我看到一个非常奇怪的行为
在 VS2008 中,我有一个简单的 WPF 应用程序:
<TextBox x:Name="textbox" Text="{Binding Path=MyProperty,Mode=TwoWay}"></TextBox>
and
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 重置为“强制值”。如果我调试,我会发现应用程序在 Coerce 函数中中断,但 UI 未更新。
有趣的是,同样的代码可以在 VS2010 中运行,UI 会使用强制值进行更新。
有人知道发生了什么事吗?
这是 WPF 错误吗?或者我错过了什么?
您必须通过强制更新UpdateTarget()
。看一眼http://social.msdn.microsoft.com/forums/en-US/wpf/thread/c404360c-8e31-4a85-9762-0324ed8812ef/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)