首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >依赖属性强制绑定问题

依赖属性强制绑定问题
EN

Stack Overflow用户
提问于 2010-06-09 14:33:05
回答 1查看 3K关注 0票数 8

我同时安装了VS2008和VS2010,我看到一个非常奇怪的行为

在VS2008中,我有一个简单的WPF应用程序:

代码语言:javascript
复制
<TextBox x:Name="textbox" Text="{Binding Path=MyProperty,Mode=TwoWay}"></TextBox>

代码语言:javascript
复制
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";
}

当我在文本框中输入随机字符串并点击Tab键时,我希望textbox.Text被重置为“强制值”。如果我调试,我看到应用程序在强制函数中中断,但UI没有更新。

有趣的是,同样的代码在VS2010中也可以工作,UI会用强制值进行更新。有谁知道发生了什么事吗?

这是一个WPF错误吗?还是我错过了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-06-09 14:40:25

您必须通过UpdateTarget()强制更新。看一看http://social.msdn.microsoft.com/forums/en-US/wpf/thread/c404360c-8e31-4a85-9762-0324ed8812ef/

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3003546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档