gpt4 book ai didi

c# - 在查看模式设置为滚动时调整 FlowDocumentReader 滚动增量?

转载 作者:太空狗 更新时间:2023-10-29 20:49:55 24 4
gpt4 key购买 nike

我正在使用 ViewingMode="Scroll"在 FlowDocumentReader 中显示 FlowDocument。如果我使用鼠标上的滚轮,文档滚动得非常慢。我想增加滚动步骤。

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

  2. 我在 FlowDocument 和 FlowDocumentReader 上添加了一个 Scroll 事件,但是当我使用鼠标滚轮时该事件不会触发。

  3. 我在 FlowDocumentReader 上添加了一个 Loaded 事件,获得了 ScrollViewer 后代,从滚动查看器的模板中找到 ScrollBar(“PART_VerticalScrollBar”)并调整 SmallChange 和 LargeChange 属性。这也没有任何效果。

有人有什么想法吗?

最佳答案

我们可以在 Control 的 MouseWheel 事件中修改它,就像 Sohnee 所说的那样,但是它只能针对一种特定情况解决,并且您必须能够访问 FlowDocumentReader,如果您使用 MVVM 之类的东西,你不会的。相反,我们可以创建一个附加属性,然后我们可以使用 ScrollViewer 在任何元素上设置该属性。在定义附加属性时,我们还需要一个 PropertyChanged 回调,我们将在其中对滚动速度执行实际修改。我还为我的属性设置了默认值 1,我要使用的速度范围是 0.1 倍到 3 倍,不过您也可以轻松地执行 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)
{
}

现在我们有了附加属性,我们需要处理滚动,为此,我们可以在 OnScrollSpeedChanged 中处理 PreviewMouseWheel 事件。我们想要连接到 PreviewMouseWheel,因为它是一个隧道事件,将在 ScrollViewer 可以处理标准的 MouseWheel 事件之前发生。

目前,PreviewMouseWheel 处理程序正在接收 FlowDocumentReader 或我们将其绑定(bind)到的其他内容,但我们需要的是 ScrollViewer。由于它可能有很多东西:ListBox、FlowDocumentReader、WPF Toolkit Grid、ScrollViewer 等,我们可以制作一个使用 VisualTreeHelper 来执行此操作的简短方法。我们已经知道通过的项目将是某种形式的 DependancyObject,因此我们可以使用一些递归来查找 ScrollViewer(如果它存在)。

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 属性。此外,我们可以使用我们的辅助方法来获取元素中包含的 ScrollViewer。一旦我们有了这两个东西,我们就可以获取和修改 ScrollViewer 的 VerticalOffset。我发现将 MouseWheelEventArgs.Delta(即鼠标滚轮的变化量)除以 6 可获得大约默认的滚动速度。所以,如果我们将它乘以我们的 ScrollSpeed 修饰符,我们就可以获得新的偏移值。然后我们可以使用它公开的 ScrollToVerticalOffset 方法设置 ScrollViewer 的 VerticalOffset。

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.");
}
}

现在我们已经设置了附加属性,我们可以创建一个简单的 UI 来演示它。我将创建一个 ListBox 和一个 FlowDocumentReaders,以便我们可以看到 ScrollSpeed 将如何跨多个控件受到影响。

<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>

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

关于c# - 在查看模式设置为滚动时调整 FlowDocumentReader 滚动增量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/876994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com