gpt4 book ai didi

c# - 依赖属性强制绑定(bind)问题

转载 作者:太空狗 更新时间:2023-10-29 19:44:38 25 4
gpt4 key购买 nike

我同时安装了 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 被重置为“强制值”。如果我调试,我会看到应用程序在 Coerce 函数中中断,但 UI 未更新。

有趣的是,同样的代码在 VS2010 中有效,UI 会更新为强制值。有人知道发生了什么事吗?

这是 WPF 错误吗?还是我遗漏了什么?

最佳答案

关于c# - 依赖属性强制绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3003546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com