调整FlowDocumentReader的滚动增量当ViewingMode设置为滚动? [英] Adjust FlowDocumentReader's Scroll Increment When ViewingMode Set to Scroll?

查看:146
本文介绍了调整FlowDocumentReader的滚动增量当ViewingMode设置为滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与ViewingMode =滚动一个FlowDocumentReader显示的FlowDocument。如果我用滚轮上我的鼠标,文件滚动很慢。我想增加滚动一步。

I'm displaying a FlowDocument in a FlowDocumentReader with the ViewingMode="Scroll". If I use the wheel on my mouse, the document scrolls very slowly. I'd like to increase the scroll step.


  1. 我试图改变我的控制面板中的鼠标滚动设置,但没有任何效果。我认为,WPF忽略设置为FlowDocumentScrollViewer。

  1. I've tried to change the Scroll setting of my mouse in Control Panel, but that doesn't have any effect. I think that WPF ignores that setting for the FlowDocumentScrollViewer.

我已经添加上的FlowDocument和FlowDocumentReader scroll事件,但是当我使用鼠标滚轮不火。

I've added a Scroll event on the FlowDocument and FlowDocumentReader, but that doesn't fire when I use the mouse wheel.

我已经添加在FlowDocumentReader一个Loaded事件,得到的ScrollViewer后裔,
从滚动观众的模板中找到的滚动条(PART_VerticalScrollBar),并调整了SmallChange和放大器; LargeChange属性。这也没有任何效果。

I've added a Loaded event on the FlowDocumentReader, got the ScrollViewer descendant, found the ScrollBar ("PART_VerticalScrollBar") from the scroll viewer's template and adjusted the SmallChange & LargeChange properties. That also didn't have any effect.

任何人有什么想法?

推荐答案

我们可以在控制的鼠标滚轮事件修改本,喜欢Sohnee sugested,但随后刚刚解决一个特定的情况下,你不得不有机会获得FlowDocumentReader,而如果你usinging像MVVM,你不会。相反,我们可以创建一个附加属性,我们可以再用的ScrollViewer任何元素上设置。当定义我们的附加属性,我们也打算要一个PropertyChanged的回调,我们将执行实际的修改滚动速度。我也给我的财产默认值为1,速度快,我要使用的范围是.1x 3倍,但你可以很容易地像做1-10。

We can modify this in a Control's MouseWheel event, like Sohnee sugested, but then it'd just be solved for one specific case, and you'd have to have access to the FlowDocumentReader, which if your usinging something like MVVM, you wont. Instead, we can create an attached property that we can then set on any element with a ScrollViewer. When defining our attached property, we also are going to want a PropertyChanged callback where we will perform the actual modifications to the scroll speed. I also gave my property a default of 1, the range of speed I'm going to use is .1x to 3x, though you could just as easily do something like 1-10.

public static double GetScrollSpeed(DependencyObject obj)
{
    return (double)obj.GetValue(ScrollSpeedProperty);
}

public static void SetScrollSpeed(DependencyObject obj, double value)
{
    obj.SetValue(ScrollSpeedProperty, value);
}

public static readonly DependencyProperty ScrollSpeedProperty =
    DependencyProperty.RegisterAttached(
    "ScrollSpeed",
    typeof(double),
    typeof(ScrollHelper),
    new FrameworkPropertyMetadata(
        1.0,
        FrameworkPropertyMetadataOptions.Inherits & FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
        new PropertyChangedCallback(OnScrollSpeedChanged)));

private static void OnScrollSpeedChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
}

现在,我们有我们的附加属性,我们需要处理滚动,要做到这一点,在OnScrollSpeed​​Changed我们可以处理previewMouseWheel事件。我们要挂钩到previewMouseWheel,因为它是以前的ScrollViewer可以处理标准鼠标滚轮事件将发生隧道的事件。

Now that we have our Attached Property we need to handle the scrolling, to do this, in the OnScrollSpeedChanged we can handle the PreviewMouseWheel event. We want to hook into the PreviewMouseWheel, since it is a tunneling event that will occur before the ScrollViewer can handle the standard MouseWheel event.

目前,在previewMouseWheel处理程序正在在FlowDocumentReader,或者我们把它绑定到其他的事情,但是我们需要的是对的ScrollViewer。因为它可能是一个很大的事情:列表框,FlowDocumentReader,WPF工具包电网,ScrollViewer中,等等,我们可以使用该VisualTreeHelper做到这一点很短的方法。我们已经知道,通过未来的项目将是某种形式的DependancyObject的,所以我们可以使用一些递归找到的ScrollViewer如果它存在。

Currently, the PreviewMouseWheel handler is taking in the FlowDocumentReader or other thing that we bound it to, however what we need is the ScrollViewer. Since it could be a lot of things: ListBox, FlowDocumentReader, WPF Toolkit Grid, ScrollViewer, etc, we can make a short method that uses the VisualTreeHelper to do this. We already know that the item coming through will be some form of DependancyObject, so we can use some recursion to find the ScrollViewer if it exists.

public static DependencyObject GetScrollViewer(DependencyObject o)
{
    // Return the DependencyObject if it is a ScrollViewer
    if (o is ScrollViewer)
    { return o; }

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(o); i++)
    {
        var child = VisualTreeHelper.GetChild(o, i);

        var result = GetScrollViewer(child);
        if (result == null)
        {
            continue;
        }
        else
        {
            return result;
        }
    }

    return null;
}

private static void OnScrollSpeedChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
    var host = o as UIElement;
    host.PreviewMouseWheel += new MouseWheelEventHandler(OnPreviewMouseWheelScrolled);
}

现在,我们可以得到ScrollViwer我们终于可以修改滚动速度。我们需要得到从正在通过发送DependancyObject的ScrollSpeed​​财产。另外,我们可以用我们的helper方法来获取包含在元素中的ScrollViewer。一旦我们有了这两样东西,我们可以得到和修改的ScrollViewer的VerticalOffset。我发现,除以MouseWheelEventArgs.Delta,它是鼠标滚轮改变,由6量获得大约默认滚动速度。因此,如果我们乘上我们的ScrollSpeed​​修改,我们就可以得到新的偏移值。然后,我们可以使用它暴露了ScrollToVerticalOffset方法设置的ScrollViewer的VerticalOffset。

Now that we can get the ScrollViwer we can finally modify the scroll speed. We'll need to get the ScrollSpeed property from the DependancyObject that is being sent through. Also, we can use our helper method to get the ScrollViewer that is contained within the element. Once we have these two things, we can get and modify the ScrollViewer's VerticalOffset. I found that dividing the MouseWheelEventArgs.Delta, which is the amount that the mouse wheel changed, by 6 gets approximately the default scroll speed. So, if we multiply that by our ScrollSpeed modifier, we can then get the new offset value. We can then set the ScrollViewer's VerticalOffset using the ScrollToVerticalOffset method that it exposes.

private static void OnPreviewMouseWheelScrolled(object sender, MouseWheelEventArgs e)
{
    DependencyObject scrollHost = sender as DependencyObject;

    double scrollSpeed = (double)(scrollHost).GetValue(Demo.ScrollSpeedProperty);

    ScrollViewer scrollViewer = GetScrollViewer(scrollHost) as ScrollViewer;

    if (scrollViewer != null)
    {
        double offset = scrollViewer.VerticalOffset - (e.Delta * scrollSpeed / 6);
        if (offset < 0)
        {
            scrollViewer.ScrollToVerticalOffset(0);
        }
        else if (offset > scrollViewer.ExtentHeight)
        {
            scrollViewer.ScrollToVerticalOffset(scrollViewer.ExtentHeight);
        }
        else
        {
            scrollViewer.ScrollToVerticalOffset(offset);
        }

        e.Handled = true;
    }
    else
    {
        throw new NotSupportedException("ScrollSpeed Attached Property is not attached to an element containing a ScrollViewer.");
    }
}

现在,我们已经得到了我们的附加属性设置,我们可以创建一个简单的用户界面来证明它。我要创建一个列表框和一个FlowDocumentReaders这样我们就可以看到如何ScrollSpeed​​将在多个控件受到影响。

Now that we've got our Attached Property set up, we can create a simple UI to demonstrate it. I'm going to create a ListBox, and a FlowDocumentReaders so that we can see how the ScrollSpeed will be affected across multiple controls.

<UniformGrid Columns="2">
    <DockPanel>
        <Slider DockPanel.Dock="Top"
            Minimum=".1"
    	    Maximum="3"
    	    SmallChange=".1"
	        Value="{Binding ElementName=uiListBox, Path=(ScrollHelper:Demo.ScrollSpeed)}" />
        <ListBox x:Name="uiListBox">
            <!-- Items -->
        </ListBox>
    </DockPanel>
    <DockPanel>
        <Slider DockPanel.Dock="Top"
		    Minimum=".1"
    		Maximum="3"
    		SmallChange=".1"
    	    Value="{Binding ElementName=uiListBox, Path=(ScrollHelper:Demo.ScrollSpeed)}" />
        <FlowDocumentReader x:Name="uiReader"
            ViewingMode="Scroll">
            <!-- Flow Document Content -->
        </FlowDocumentReader>
    </DockPanel>
</UniformGrid>

现在,在运行时,我们可以使用滑块来修改滚动速度在每一列,有趣的东西。

Now, when run, we can use the Sliders to modify the scrolling speed in each of the columns, fun stuff.

这篇关于调整FlowDocumentReader的滚动增量当ViewingMode设置为滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆