需要创建一个具有标题和滚动条支持的上下文菜单
我需要一个具有标题和各种菜单项的全局上下文菜单 style
/template
;由于上下文菜单中的菜单项数量可能很大,因此需要支持滚动。
我当前样式的问题是它不支持滚动;即使菜单项的数量超过屏幕尺寸,也不会显示滚动条。
这是我正在使用的当前样式 -
<Style
TargetType="{x:Type ContextMenu}"
x:Key="ContextMenuStyle">
<Setter
Property="ContextMenu.Template">
<Setter.Value>
<ControlTemplate>
<Border
BorderBrush="#868686"
BorderThickness="1"
Background="#FAFAFA">
<StackPanel
Orientation="Vertical">
<Label
Foreground="White"
Background="Blue">
<Binding
RelativeSource=
"{RelativeSource AncestorType=
{x:Type ContextMenu}}"
Path="PlacementTarget.Tag" />
</Label>
<Grid>
<Rectangle
Margin="1,1,1,1"
Width="25"
HorizontalAlignment="Left"
Fill="#E9EEEE" />
<Rectangle
Margin="26,1,0,1"
Width="1"
HorizontalAlignment="Left"
Fill="#C5C5C5" />
<Rectangle
Margin="27,1,0,1"
Width="1"
HorizontalAlignment="Left"
Fill="#FAFAFA" />
<ScrollViewer
Margin="1,0,1,0"
Style="{DynamicResource
{ComponentResourceKey
ResourceId=MenuScrollViewer,
TypeInTargetAssembly=
{x:Type FrameworkElement}}}"
CanContentScroll="True"
Grid.ColumnSpan="2">
<ItemsPresenter
KeyboardNavigation.DirectionalNavigation=
"Cycle" />
</ScrollViewer>
</Grid>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
将滚动查看器放置在标题上方可以工作,但标题也会滚动。
实现这一目标的最佳方法是什么?
I need a global context menu style
/template
having a header and then various menu items; as the number of menu items in my context menu can be large it needs to support the scrolling.
The problem with current style I have is that it does not support the scrolling; even when the number of menu items grows past the screen size no scroll bar is displayed.
Here is the current style I am using -
<Style
TargetType="{x:Type ContextMenu}"
x:Key="ContextMenuStyle">
<Setter
Property="ContextMenu.Template">
<Setter.Value>
<ControlTemplate>
<Border
BorderBrush="#868686"
BorderThickness="1"
Background="#FAFAFA">
<StackPanel
Orientation="Vertical">
<Label
Foreground="White"
Background="Blue">
<Binding
RelativeSource=
"{RelativeSource AncestorType=
{x:Type ContextMenu}}"
Path="PlacementTarget.Tag" />
</Label>
<Grid>
<Rectangle
Margin="1,1,1,1"
Width="25"
HorizontalAlignment="Left"
Fill="#E9EEEE" />
<Rectangle
Margin="26,1,0,1"
Width="1"
HorizontalAlignment="Left"
Fill="#C5C5C5" />
<Rectangle
Margin="27,1,0,1"
Width="1"
HorizontalAlignment="Left"
Fill="#FAFAFA" />
<ScrollViewer
Margin="1,0,1,0"
Style="{DynamicResource
{ComponentResourceKey
ResourceId=MenuScrollViewer,
TypeInTargetAssembly=
{x:Type FrameworkElement}}}"
CanContentScroll="True"
Grid.ColumnSpan="2">
<ItemsPresenter
KeyboardNavigation.DirectionalNavigation=
"Cycle" />
</ScrollViewer>
</Grid>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Placing hte scroll viewer above header works but then header is also scrolled.
What is the best way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这样吧。将垂直
StackPanel
(不限制高度)更改为具有两个 RowDefinitions 的网格 (Auto, *
)Try it like this instead. Changed the Vertical
StackPanel
(which doesn't restrict the Height) to a Grid with two RowDefinitions (Auto, *
)试试这个:
只需将您的 stackpanel 替换为 dockpanel
try this:
Simply replace your stackpanel to dockpanel