gpt4 book ai didi

WPF - 如何替换滚动条 ContextMenu

转载 作者:行者123 更新时间:2023-12-04 22:17:40 27 4
gpt4 key购买 nike

我正在尝试为 ScrollBars 替换 ContextMenu,我已经编写了这段代码:

<ContextMenu x:Key="ScrollBarContextMenu" x:Shared="True">
<MenuItem Header="Scroll _Here" Name="SH" Command="ScrollBar.ScrollHereCommand" />
<Separator/>
<MenuItem Header="_Top" Name="T" Command="ScrollBar.ScrollToTopCommand" />
<MenuItem Header="_Bottom" Name="B" Command="ScrollBar.ScrollToBottomCommand" />
<Separator/>
<MenuItem Header="Page _Up" Name="PU" Command="ScrollBar.PageUpCommand" />
<MenuItem Header="Page _Down" Name="PD" Command="ScrollBar.PageDownCommand" />
<Separator/>
<MenuItem Header="Scroll U_p" Name="SU" Command="ScrollBar.LineUpCommand" />
<MenuItem Header="Scroll Dow_n" Name="SD" Command="ScrollBar.LineDownCommand" />
</ContextMenu>


<Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="ContextMenu" Value="{DynamicResource ScrollBarContextMenu}"/>
<Style.Triggers>
<Trigger Property="Orientation" Value="Horizontal">
<Setter Property="Width" Value="Auto"/>
<Setter Property="Height" Value="18" />
<Setter Property="Template" Value="{StaticResource HorizontalScrollBar}" />
</Trigger>
<Trigger Property="Orientation" Value="Vertical">
<Setter Property="Width" Value="18"/>
<Setter Property="Height" Value="Auto" />
<Setter Property="Template" Value="{StaticResource VerticalScrollBar}" />
</Trigger>
</Style.Triggers>
</Style>

上下文菜单已设置,但它的行为很奇怪。最初它的所有菜单项都被禁用。当您滚动滚动条时,除了永远禁用的 ScrollHere 命令外,它们都被启用。此外,当单击一个选项时,即“Page Up”选项,它仅在托管滚动条的控件获得焦点时起作用(它不会自动获得焦点)。有谁知道如何解决这些问题?

编辑:我的猜测是,也许默认的 ContextMenu 会处理 Opening 事件并使控件聚焦,而且它还会将鼠标单击点的位置存储在某处。但是我怎样才能把这个功能放在 XAML 文件中呢???

最佳答案

好的。以下是您的操作方法:

        <ContextMenu x:Key="VScrollBarContextMenu" x:Shared="true">
<MenuItem Header="{DynamicResource ScrollHere}" Command="ScrollBar.ScrollHereCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"/>
<Separator/>
<MenuItem Header="{DynamicResource ScrollTop}" Command="ScrollBar.ScrollToTopCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
<MenuItem Header="{DynamicResource ScrollBottom}" Command="ScrollBar.ScrollToBottomCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
<Separator/>
<MenuItem Header="{DynamicResource ScrollPageUp}" Command="ScrollBar.PageUpCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
<MenuItem Header="{DynamicResource ScrollPageDown}" Command="ScrollBar.PageDownCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
<Separator/>
<MenuItem Header="{DynamicResource ScrollUp}" Command="ScrollBar.LineUpCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
<MenuItem Header="{DynamicResource ScrollDown}" Command="ScrollBar.LineDownCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
</ContextMenu>

<ContextMenu x:Key="HScrollBarContextMenu" x:Shared="true">
<MenuItem Header="{DynamicResource ScrollHere}" Command="ScrollBar.ScrollHereCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"/>
<Separator/>
<MenuItem Header="{DynamicResource ScrollLeftEnd}" Command="ScrollBar.ScrollToLeftEndCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
<MenuItem Header="{DynamicResource ScrollRightEnd}" Command="ScrollBar.ScrollToRightEndCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
<Separator/>
<MenuItem Header="{DynamicResource ScrollPageLeft}" Command="ScrollBar.PageLeftCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
<MenuItem Header="{DynamicResource ScrollPageRight}" Command="ScrollBar.PageRightCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
<Separator/>
<MenuItem Header="{DynamicResource ScrollLeft}" Command="ScrollBar.LineLeftCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
<MenuItem Header="{DynamicResource ScrollRight}" Command="ScrollBar.LineRightCommand" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" />
</ContextMenu>

我缺少命令目标...

关于WPF - 如何替换滚动条 ContextMenu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14691687/

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