需要创建一个具有标题和滚动条支持的上下文菜单

发布于 2024-11-29 09:37:12 字数 2937 浏览 0 评论 0原文

我需要一个具有标题和各种菜单项的全局上下文菜单 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄与心 2024-12-06 09:37:13

试试这样吧。将垂直 StackPanel (不限制高度)更改为具有两个 RowDefinitions 的网格 (Auto, *)

<Style TargetType="{x:Type ContextMenu}">
    <Setter Property="ContextMenu.Template">
        <Setter.Value>
            <ControlTemplate>
                <Border BorderBrush="#868686"
                        BorderThickness="1"
                        Background="#FAFAFA">
                    <Grid VerticalAlignment="Stretch">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Label Grid.Row="0" Foreground="White" Background="Blue">
                            <Binding RelativeSource= "{RelativeSource AncestorType= {x:Type ContextMenu}}" Path="PlacementTarget.Tag" />
                        </Label>
                        <Grid Grid.Row="1">
                            <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>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Try it like this instead. Changed the Vertical StackPanel (which doesn't restrict the Height) to a Grid with two RowDefinitions (Auto, *)

<Style TargetType="{x:Type ContextMenu}">
    <Setter Property="ContextMenu.Template">
        <Setter.Value>
            <ControlTemplate>
                <Border BorderBrush="#868686"
                        BorderThickness="1"
                        Background="#FAFAFA">
                    <Grid VerticalAlignment="Stretch">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Label Grid.Row="0" Foreground="White" Background="Blue">
                            <Binding RelativeSource= "{RelativeSource AncestorType= {x:Type ContextMenu}}" Path="PlacementTarget.Tag" />
                        </Label>
                        <Grid Grid.Row="1">
                            <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>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
天涯沦落人 2024-12-06 09:37:13

试试这个:

    <Border>
        <DockPanel>
            <Label DockPanel.Dock="Top">Label</Label>
            <ScrollViewer>
                ....    
            </ScrollViewer>
        </DockPanel>
    </Border>

只需将您的 stackpanel 替换为 dockpanel

try this:

    <Border>
        <DockPanel>
            <Label DockPanel.Dock="Top">Label</Label>
            <ScrollViewer>
                ....    
            </ScrollViewer>
        </DockPanel>
    </Border>

Simply replace your stackpanel to dockpanel

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文