从ViewModel控制TextBox的滚动位置?

时间:2012-02-29 18:07:43

标签: c# wpf mvvm

是否有一个涉及WPF TextBox / Block的解决方案,它通过绑定自动滚动到最后?这显然可以通过直接调用控件在后面的代码中完成,但是如何使用Binding和MVVM做到这一点?

在后面的代码中工作(但我想避免这种情况并使用VM来做所有事情)

  public void _readerService_BytesArrived(string s)
  {
     Action dispatcherAction = () =>
     {
        txtBoxOutPut.AppendText(s);
        txtBoxOutPut.ScrollToEnd();
     };
     Dispatcher.Invoke(dispatcherAction);
  }

2 个答案:

答案 0 :(得分:3)

我想你正在尝试滚动到文本的值在TextBox / Block内部更改时结束。由于这是与视图相关的操作,因此它应该保持这种状态。只需在控件上放置一个TextChanged事件,并在Text属性更改时滚动到结尾。


请注意,这基本上意味着您需要拆分操作...在视图模型端保持绑定,并将ScrollToEnd放在视图后面的视图中...视图模型不应该关心谁消耗文本字符串以及它们的行为方式。

答案 1 :(得分:1)

System.Windows.Interactivity行为可能只适合您。我使用它们来滚动各种控件,它不在VM中,但它也不在视图中并跟随MVVM。

以下是可能有用的Scrollviewer示例

    public class FrameworkElementScrollviewerScrollingBehavior : Behavior<FrameworkElement>
{
    private FrameworkElement _AssociatedElement;

    private ScrollViewer _listboxScrollViewer = null;

    #region OnAttached
    protected override void OnAttached()
    {
        base.OnAttached();
        _AssociatedElement = AssociatedObject;
        _AssociatedElement.Loaded += OnControlLoaded;
        _AssociatedElement.Unloaded += new RoutedEventHandler(_AssociatedElement_Unloaded);

        //TODO: register/subscrive for event/message from the VM that tells you the scrollviewer to do something
    }

    //TODO: handle the event using the _AssociatedElement as the control you are acting on

    void _AssociatedElement_Unloaded(object sender, RoutedEventArgs e)
    {
        Cleanup();
    }
    #endregion

    #region OnDetaching
    protected override void OnDetaching()
    {
        Cleanup();
        base.OnDetaching();
    }
    #endregion

    private bool _isCleanedUp;
    private void Cleanup()
    {
        if (!_isCleanedUp)
        {
            _AssociatedElement.Loaded -= OnControlLoaded;
            _AssociatedElement.Unloaded -= _AssociatedElement_Unloaded;
        }
    }


    #region OnControlLoaded
    private void OnControlLoaded(object sender, RoutedEventArgs args)
    {
        FrameworkElement element = sender as FrameworkElement;
        if (element != null)
        {
            _listboxScrollViewer = GetDescendantByType(sender as Visual, typeof(ScrollViewer)) as ScrollViewer;

            if (_listboxScrollViewer.ComputedVerticalScrollBarVisibility == Visibility.Visible)
                //do something when content is scrollable
        }
    }
    #endregion

    #region GetDescendantByType
    /// <summary>
    /// Gets the descendent of type
    /// </summary>
    /// <param name="element">The element.</param>
    /// <param name="type">The type.</param>
    /// <returns></returns>
    public static Visual GetDescendantByType(Visual element, Type type)
    {
        if (element == null) return null;

        if (element.GetType() == type) return element;
        Visual foundElement = null;
        if (element is FrameworkElement)
            (element as FrameworkElement).ApplyTemplate();
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
        {
            Visual visual = VisualTreeHelper.GetChild(element, i) as Visual;
            foundElement = GetDescendantByType(visual, type);
            if (foundElement != null)
                break;
        }
        return foundElement;
    }
    #endregion
}