WPF .net 4.0:与 UpdateSourceTrigger=PropertyChanged 绑定时,文本框重置插入符号位置

发布于 2024-12-12 09:38:50 字数 485 浏览 0 评论 0原文

我有以下情况:

  • 绑定到属性的 TextBox:
;
  • 属性 ClientName 将其值存储在底层结构中,并且不会在属性更改事件上调用 Notify。相反,底层结构在处理该值后发送一个事件来刷新 UI。如果触发此类事件,则会正确设置 ClientNameProperty 并为此属性调用 Notify。

问题是,如果我输入任何文本,插入符号似乎会跳转到文本框中的第一个位置,实际上会反转我输入的任何字符串。 “abcd”变成“dcba”

我注意到这种行为是在我们迁移到 Net 4.0 后发生的。

那里有什么好的解决方案吗? 非常感谢

I've got the following Situation:

  • TextBox which is bound to a property:
<TextBox Text="{Binding Settings.ClientName, UpdateSourceTrigger=PropertyChanged}"/>
  • The property ClientName stores its value in the unterlying structures and does NOT call Notifyon the property changed event. instead the underlying structures send an event to refresh the UI after they processed the value . If such an event is fired, the ClientNameProperty is set correctly and Notify is called for this property.

the problem is that if i enter any text, the caret seems to jump to the first position in the textbox, actuall reversing any string i enter. "abcd" becomes "dcba"

I noticed that this behaviour occured after we migrated to Net 4.0.

Are there any good solutions out there?
Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

油焖大侠 2024-12-19 09:38:51

没有内置行为可以做到这一点。这个问题很可能来自于你的处理。

顺便说一句,如果您希望 TextBox 从代码隐藏以及用户输入中更改,则需要将其设为双向:

<TextBox Text="{Binding Settings.ClientName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

编辑:
您还可以将 ClientName 设置为依赖属性(vs2010 中的 propdp 片段)。这将自动完全支持绑定(/样式/动画),并让您可以在值更改时采取行动以及通过回调委托将其强制返回。
最后但并非最不重要的一点是,您仍然不需要 Settings 类来实现 INotifyPropertyChanged
这里是一个起点 (msdn)。

There is no built-in behavior that would do that. That problem is likely to come from your processing.

On a side note, if you want the TextBox to change from code-behind as well as from user input, you want to make it Two-Way:

<TextBox Text="{Binding Settings.ClientName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

EDIT:
You could also make the ClientName a dependency property (propdp snippet in vs2010). That would automatically fully support binding(/styling/animation), and gives you the possibility to act when the value changes as well as to coerce it back through callback delegates.
Last but not least, you still wouldn't need your Settings class to implement INotifyPropertyChanged.
Here is a place to start (msdn).

寄与心 2024-12-19 09:38:51

我怀疑您在 WPF TextBox 中遇到了这种行为更改: https://connect.microsoft.com/VisualStudio/feedback/details/588343/changed-behaviour-from-net-3-5-to-net-4-0-of-wpf-textbox- formatting-when-propertychanged-is-used-as-updatesourcetrigger(将 WPF TextBox 格式设置的行为从 .Net 3.5 更改为 .Net 4.0) PropertyChanged用作UpdateSourceTrigger)

I suspect you are suffering from this behavior change in the WPF TextBox: https://connect.microsoft.com/VisualStudio/feedback/details/588343/changed-behaviour-from-net-3-5-to-net-4-0-of-wpf-textbox-formatting-when-propertychanged-is-used-as-updatesourcetrigger (Changed behaviour from .Net 3.5 to .Net 4.0 of WPF TextBox formatting when PropertyChanged is used as UpdateSourceTrigger)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文