捕获文本框的WPF滚动条上的鼠标点击

本文关键字:鼠标 滚动条 WPF 文本 | 更新日期: 2023-09-27 18:18:40

我有一个WPF ScrollingTextBox定义如下。这个文本框是只读的,当ViewModel中的事件被触发时,它的内容会被更新。

<Grid>
    <StackPanel>
        <local:ScrollingTextBox x:Name="textBox1"
                                Width="480"
                                Height="100"
                                Margin="12,12,0,0"
                                HorizontalAlignment="Left"
                                VerticalAlignment="Top"
                                IsReadOnly="True"
                                Background="Black"
                                Foreground="White"
                 Text="{Binding Path=UpdatedText, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"/>
    </StackPanel>
</Grid>

我必须定义以下类,以便在向文本框中添加新文本时启用自动向下滚动。此外,我需要重写OnPreviewMouseLeftButtonDownOnPreviewMouseLeftButtonUp方法,以便在用户单击滚动条时禁用自动向下滚动:这些方法和FindVisualParent方法中包含的说明来自本页。

public class ScrollingTextBox : TextBox
{
    private volatile bool _AutomaticScrollingEnabled = true;
    protected override void OnInitialized(EventArgs e)
    {
        base.OnInitialized(e);
        VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
        HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
    }
    protected override void OnTextChanged(TextChangedEventArgs e)
    {
        base.OnTextChanged(e);
        if (_AutomaticScrollingEnabled)
        {
            Focus();
            CaretIndex = Text.Length;
            ScrollToEnd();
        }
    }
    protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        base.OnPreviewMouseLeftButtonDown(e);
        object original = e.OriginalSource;
        if (!original.GetType().Equals(typeof(ScrollViewer)))
        {
            if (FindVisualParent<ScrollBar>(original as DependencyObject) != null)
            {
                _AutomaticScrollingEnabled = false;
                Trace.WriteLine("scroll bar is clicked down");
            }
        }
    }
    protected override void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e)
    {
        base.OnPreviewMouseLeftButtonUp(e);
        object original = e.OriginalSource;
        if (!original.GetType().Equals(typeof(ScrollViewer)))
        {
            if (FindVisualParent<ScrollBar>(original as DependencyObject) != null)
            {
                _AutomaticScrollingEnabled = true;
                Trace.WriteLine("scroll bar is clicked up");
            }
        }
    }
    private parentItem FindVisualParent<parentItem>(DependencyObject obj) where parentItem : DependencyObject
    {
        DependencyObject parent = VisualTreeHelper.GetParent(obj);
        while (parent != null && !parent.GetType().Equals(typeof(parentItem)))
        {
            parent = VisualTreeHelper.GetParent(parent);
        }
        return parent as parentItem;
    }
}
  1. 为什么我需要验证事件原始源是否不等于typeof(ScrollViewer) ?
  2. FindVisualParent方法是如何工作的?

捕获文本框的WPF滚动条上的鼠标点击

为什么我需要验证事件原始源是否不等于typeof(ScrollViewer)?

我认为关键是要确定用户在ScrollViewer中单击的确切位置。因此,如果ScrollViewer本身正在发送事件,那是没有用的,您希望等到事件传递到您感兴趣的特定子节点之一。(请注意,PreviewMouseLeftButtonUpPreviewMouseLeftButtonDown是隧道事件,这意味着事件从根向下通过可视化树,直到它们被处理。)

FindVisualParent方法是如何工作的?

它只是沿着可视化树往上爬,直到找到指定类型的元素("parentItem"),或者到达顶部。

private parentItem FindVisualParent<parentItem>(DependencyObject obj) 
    where parentItem : DependencyObject
{
    // start by getting the parent of the input element
    DependencyObject parent = VisualTreeHelper.GetParent(obj);
    // repeat until there is no parent, or the current element matches the target type
    while (parent != null && !parent.GetType().Equals(typeof(parentItem)))
    {
        // get the next element up
        parent = VisualTreeHelper.GetParent(parent);
    }
    // return the found element of type "parentItem", or null if not found
    return parent as parentItem;
}