在 WPF 文本框中自定义撤消/重做,并进行适当的插入符号移动

发布于 2024-11-25 09:43:56 字数 580 浏览 3 评论 0原文

我已经实现了自定义撤消/重做堆栈,并尝试让它与 WPF TextBox 一起使用。

我已经关闭了内置的撤消机制,并在 Ctrl+Z 上连接了我的自定义撤消。一切工作正常,接受每次撤消/重做时文本框中的插入符号始终移动到索引 0。问题是如何解决这个问题?

我尝试在 TextBox 上设置自定义行为,该行为侦听 TextChanged 并本地化文本字符串中的最后一个更改。但这只有在您开始连续多次输入相同的字母时才有效。我的方法失败了。

我理想地想要的是某种只对 TextBox.Text 属性进行实际更改的行为。就像现在一样,每次撤消都会完全更新,即使只是删除了最后输入的字母。这当然并不奇怪,因为它监听我的PresentationModel上的Text属性,该属性在撤消时触发PropertyChanged。

但是,如果有一些更详细的方法来准确说明属性值发生了什么变化,即在字符串值中仅插入/删除一个或几个字母,那不是很好吗?然后 TextBox 只能更改它,而不必刷新其整个 Text-value。是否有任何这样的方式来告诉文本框这一切都准备好了,或者是否可以制作一个以这种方式表现的自定义文本框?然后就可以查明新插入符号的确切位置,而不必在每次属性更改更新时都直接返回到 0!

I have implemented a custom Undo/Redo stack and Im trying to get it to work with the WPF TextBox.

I have turned off the built in Undo-mechanism and hooked up my custom Undo on Ctrl+Z. Everything works fine accept that the caret in the TextBox is always being moved to index 0 on every undo/redo. The question is how to solve this?

I have tried having a custom behaviour on the TextBox which listens to TextChanged and is localizing the last change in the text-string. But this only works unless you start typing the same letter several times in a row. The my method breaks down.

What I ideally want is some kind of behaviour that only makes actual changes to the TextBox.Text-property. As it is now it is updated completely for every Undo, even if its only the last entered letter that is removed. This is of course no suprise since it listens to the Text-property on my PresentationModel which is triggering PropertyChanged on Undo.

But wouldnt it be great if there was some more fine-detailed way of telling exactly what had changed with the property-value, that only one or a couple of letters where inserted/removed in the string value. Then the TextBox could change only that without having to refresh its entire Text-value. Is there any such way of telling the TextBox this allready or could it be possible to make a custom TextBox that behaved in this way? Then it would be possible to pinpoint the exact location for the new caret without having it go straight back to 0 for every propertychange-update!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文