简体   繁体   English

如何使用caliburn.micro和视图模型在text_changed事件中执行TextBox.scrolltoend

[英]How to execute TextBox.scrolltoend in text_changed event using caliburn.micro using view model

I am using caliburn micro in my Wpf application. 我在Wpf应用程序中使用caliburn micro。 I would like to scroll down a text box when it's content is more. 当内容更多时,我想向下滚动一个文本框。 I don't want to use view's code behind to achieve this functionality. 我不想在后面使用View的代码来实现此功能。 I have google it and found the following link. 我在Google上找到了以下链接。

Use view's code behind directly . 直接在后面使用view的代码 Though it may be a workaround, I am not happy with this approach because I consider this is the starting point of bad practice. 尽管这可能是一种解决方法,但我对这种方法并不满意,因为我认为这是不良做法的起点。

I found one more reference to achieve similar functionality using Rx (reactive extensions) 我发现了另一个参考,以使用Rx(反应性扩展)实现类似的功能

Reactive Extensions for .NET (Rx) in WPF - MVVM WPF-MVVM中.NET(Rx)的反应性扩展

I don't know how to use it in the context of Caliburn Micro. 我不知道如何在Caliburn Micro中使用它。

Similar question was asked by another stackoverflow member here however no satisfactory response. 另一个stackoverflow成员在此处提出了类似的问题,但是没有令人满意的答复。

I have to do two things. 我必须做两件事。

  1. Subscribe Text Changed event 订阅文本更改事件
  2. Get the Textbox object from sender parameter (or somehow the textbox object) and execute ScrollToEnd() method of it 从sender参数(或文本框对象)获取Textbox对象,并执行其ScrollToEnd()方法

I guess it is possible either by IHandle of Caliburn micro or Rx(Reactive extensions) . 我猜这可能是由Caliburn micro的IHandleRx(Reactive extensions)造成的 Could some one please help me how to achieve this functionality? 有人可以帮我如何实现这一功能吗?

Have you looked at using an IResult? 您是否看过使用IResult? They provide a way to accomplish this without coupling the view and viewmodel together. 它们提供了一种无需将视图和视图模型耦合在一起即可实现此目的的方法。

A blurb from the docs: 来自文档的摘要:

Because coroutines occur inside of an Action, we provide you with an ActionExecutionContext useful in building UI-related IResult implementations. 由于协程发生在Action内部,因此我们为您提供了一个ActionExecutionContext,可用于构建与UI相关的IResult实现。 This allows the ViewModel a way to declaratively state it intentions in controlling the view without having any reference to a View or the need for interaction-based unit testing. 这使ViewModel可以声明性地声明其在控制视图中的意图,而无需引用View或进行基于交互的单元测试。

A example of playing a sound in SL with MediaElement and CM. 使用MediaElement和CM在SL中播放声音的示例。 Playing a sound in Silverlight with MediaElement and Caliburn Micro 使用MediaElement和Caliburn Micro在Silverlight中播放声音

Like my said in his comment, this is essentially a View operation and not a ViewModel's. 就像我在他的评论中所说,这本质上是一个View操作,而不是ViewModel。 Code behind on views is not immediately bad practice. 视图上的代码背后并不是立即的坏习惯。 Code behind on view is only bad when the operations needs dependencies past the View. 仅当操作需要视图之外的依赖项时,视图后的代码才是不好的。

If you don't like code behind on the view, you can do it by using behaviors: 如果您不喜欢视图上的代码,可以使用以下行为来实现:

  public class TextBoxScrollToEndOnTextChanged:Behavior<TextBox>
  {
    protected override void OnAttached()
    {
      AssociatedObject.TextChanged += AssociatedObject_TextChanged;
    }

    protected override void OnDetaching()
    {
      AssociatedObject.TextChanged -= AssociatedObject_TextChanged;
    }

    void AssociatedObject_TextChanged(object sender, TextChangedEventArgs e)
    {
      AssociatedObject.ScrollToEnd();
    }
  }

xaml: xaml:

<TextBox>
    <i:Interaction.Behaviors>
        <behaviors:TextBoxScrollToEndOnTextChanged />
    </i:Interaction.Behaviors>
</TextBox>

Now if you have View - ViewModel interaction, I think this is the best way to do it. 现在,如果您具有View-ViewModel交互功能,我认为这是最好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Caliburn.Micro MVVM 正确设置 WPF 文本框的样式? - How to properly style a WPF TextBox using Caliburn.Micro MVVM? 如何使用Caliburn.Micro MVVM在WPF中的同一视图上从另一个视图模型更新list &lt;&gt;? - How to update list<> from another View model on same View in WPF using Caliburn.Micro MVVM? 如何使用Caliburn.Micro选择TextBoxs中的所有文本? - How to select all text in TextBoxs using Caliburn.Micro? 将ProgressBar与Caliburn.micro一起使用 - Using ProgressBar with Caliburn.micro 如何使用WinRT Caliburn.Micro将参数传递给导航视图模型? - How to pass parameter to navigated view model with WinRT Caliburn.Micro? 使用WPF和Caliburn.Micro在视图中添加多个视图 - Add multiple views inside a view using WPF and Caliburn.Micro 将Caliburn.Micro与Mahapps MetroTabItem结合使用不会将视图加载到ContentControl - Using Caliburn.Micro with Mahapps MetroTabItem not loading View to ContentControl 使用caliburn.micro wpf c#更改视图名称 - Change name of view using caliburn.micro wpf c# 使用Caliburn.Micro将文本文件信息绑定到DataGrid - Binding a text file information to DataGrid using Caliburn.Micro 如何使用caliburn.micro更改Metro主题 - how to change metro theme using caliburn.micro
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM