滚动wpf文本块结束

时间:2013-02-19 06:38:17

标签: wpf mvvm binding datagrid

是否有TextBlock的任何功能允许滚动到最后?

我在背后的代码中看到了很多这样做的例子,

我想保持MVVM的原则而不是触及背后的代码,

我正在寻找在XAML中执行此操作的方法。

有一个吗?

1 个答案:

答案 0 :(得分:5)

我假设您的TextBlock嵌套在ScrollViewer中。在这种情况下,您将不得不创建附加属性。请参阅此相关问题:

How to scroll to the bottom of a ScrollViewer automatically with Xaml and binding?

即。创建附加属性:

public static class Helper
{
    public static bool GetAutoScroll(DependencyObject obj)
    {
        return (bool)obj.GetValue(AutoScrollProperty);
    }

    public static void SetAutoScroll(DependencyObject obj, bool value)
    {
        obj.SetValue(AutoScrollProperty, value);
    }

    public static readonly DependencyProperty AutoScrollProperty =
        DependencyProperty.RegisterAttached("AutoScroll", typeof(bool), typeof(Helper), new PropertyMetadata(false, AutoScrollPropertyChanged));

    private static void AutoScrollPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var scrollViewer = d as ScrollViewer;

        if (scrollViewer != null && (bool)e.NewValue)
        {
            scrollViewer.ScrollToBottom();
        }
    }
}

然后绑定如下:

<ScrollViewer local:Helper.AutoScroll="{Binding BooleanViewModelPropertyThatTriggersScroll}" .../>