WPF .net 4.0:与 UpdateSourceTrigger=PropertyChanged 绑定时,文本框重置插入符号位置
我有以下情况:
- 绑定到属性的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有内置行为可以做到这一点。这个问题很可能来自于你的处理。
顺便说一句,如果您希望 TextBox 从代码隐藏以及用户输入中更改,则需要将其设为双向:
编辑:
您还可以将 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:
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).
我怀疑您在 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)