从视图模型类调用RichTextBox.ScrollToEnd() [英] Call RichTextBox.ScrollToEnd() from View Model class
问题描述
您好,我尝试解决此问题,在WPF中,我使用Caliburn Micro框架。在View中,我具有可绑定的richtextbox控件,我从FlowDocument的视图模型类属性类型进行绑定。
Hi I try solve this, in WPF I use Caliburn Micro framework. In View I have bindable richtextbox control, I bind from view model class property type of FlowDocument.
我需要一种方法来在view中的richetextbox控件上调用ScrollToEnd方法
I need have a way how can I call method ScrollToEnd on richetextbox control in view.
有可能吗?因为在视图模型类中我没有richtextbox的实例。
Is it possible? Because in view model class I don’t have instance of richtextbox.
感谢您提出想法。
推荐答案
有时,有必要从文件后面的代码中调用UI代码(如果其他解决方案使事情复杂化)。请记住:模式只是建议而不是脚本。甚至有人可能会争辩说,从视图模型中公开 FlowDocument
会使视图模型过于了解UI。
Sometimes it makes sense to call UI code from the code behind file (if other solutions complicate things). Remember: patterns are just recommendations not the script. One could even argue that exposing a FlowDocument
from view model makes the view model too aware about UI. But you did it because it was easier, right?
如果您不想从后面的代码进行此调用,则有两个选择:
If you don't want to make this call from code behind, here are two options:
- 考虑使用一种
ScrollToEnd()
方法向视图模型注入接口。 View实现了此方法,View模型在需要时调用它。 - 从视图模型
ScrollToEndRequested
中公开一个事件。 View订阅此事件并在触发事件时采取相应的措施。
- Consider injecting an interface to the view model, with one
ScrollToEnd()
method. View implements this method and view model calls it whenever it feels necessity. - Expose an event from view model
ScrollToEndRequested
. View subscribes to this event and acts accordingly whenever it's fired.
其他选项(如附加行为)可能更合适,但它们确实取决于您的情况。
Other options (like attached behaviors) might be more suitable, but they are really depend on your context.
这篇关于从视图模型类调用RichTextBox.ScrollToEnd()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!