依赖性强制约束性问题 [英] Dependency Property Coercion binding issues
问题描述
在VS2008中,我有一个简单的WPF应用程序:
< TextBox x:Name =textboxText ={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));
私有静态对象Coerce(DependencyObject d,object baseValue)
{
返回强制值;
}
当我在文本框和命中选项卡中输入随机字符串时,我希望文本框。文本被重置为强制值。如果我调试,我看到应用程序在Coerce功能中断开,但UI没有更新。
有趣的是,同样的代码在VS2010中有效,UI被更新为强制值。
任何人都有一个想法发生了什么?
是WPF错误吗?或者我缺少某些东西?
您必须通过 UpdateTarget()$强制更新c $ c>。看看 http ://social.msdn.microsoft.com/forums/en-US/wpf/thread/c404360c-8e31-4a85-9762-0324ed8812ef/
I have both VS2008 and VS2010 installed, and I see a very strange behavior
In VS2008, I have a simple WPF app:
<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";
}
When I enter random string in textbox and hit tab, I expect the textbox.Text to be reset to "Coerced Value". If I debug I see that the app breaks in the Coerce function but the UI is not updated.
Interestingly this same code works in VS2010, the UI gets updated with Coerced value. Anybody has an idea whats happening?
Is it a WPF bug? or am I missing something?
You have to force an update via UpdateTarget()
. Take a look at http://social.msdn.microsoft.com/forums/en-US/wpf/thread/c404360c-8e31-4a85-9762-0324ed8812ef/
这篇关于依赖性强制约束性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!