gpt4 book ai didi

c# - 在 OverFlow 的情况下向下滚动 ContextMenu

转载 作者:太空宇宙 更新时间:2023-11-03 16:26:45 24 4
gpt4 key购买 nike

我正在 WPF 中创建应用。

我使用“System.Windows.Forms.NotifyIcon”包含了一个系统托盘图标

代码如下:

// Create WinForm notify icon
m_NotifyIcon = new System.Windows.Forms.NotifyIcon();
m_NotifyIcon.Icon = Properties.Resources.rocket;
m_NotifyIcon.Visible = true;
// Default Balloon title
m_NotifyIcon.BalloonTipTitle = "Greatest App ever";
m_NotifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu();

// Append default menu items
List<MenuItem> itemList = new List<MenuItem>();
itemList.Insert(0, new MenuItem("Exit", OnExit_Click));
itemList.Insert(0, new MenuItem("-"));
itemList.Insert(0, new MenuItem("Refresh", RefreshConsoleList));
itemList.Insert(0, new MenuItem("Filter: \"" + (String.IsNullOrEmpty(m_Filter) ? "NONE" : m_Filter) + "\"", ChangeFilter_Click));
itemList.Insert(0, new MenuItem("-"));
m_NotifyIcon.ContextMenu.MenuItems.AddRange(itemList.ToArray());

结果是这样的:

Before Refreshing

在刷新的情况下,我的应用程序会得到很多条目,然后将这些条目附加到 ContextMenu 中,菜单将如下所示:

Overflow of MenuItem

如您所见,MenuItem 过多,由于溢出,将显示 2 个箭头(ContextMenu 的顶部和底部)

现在如果用户想要退出应用程序,他必须向下滚动,然后单击退出。如果列表真的很大,可能会惹恼用户。

为了避免这种情况,我想在弹出时显示已经滚动到底部的 ContextMenu(第一个 MenuItem 可见)

但我没有找到任何事件或控件可用于以编程方式向下滚动 ContextMenu。

可以吗?

问候

伊夫·德格劳佩斯


PS:由于我的声誉(这是我的第一篇文章),我无法直接发布图片,并且发布了 2 个以上的链接。

第三个例子在这里:

yves.desgraupes.free.fr/shared/ContextMenu_OverflowScroll.png

最佳答案

您可以做的是使用 NotifyIcon 的 WPF 实现。

然后您可以拥有一个 WPF 菜单,并且可以更好地控制如何在菜单中实现您想要的行为。

因此您可以为 ContextMenu 定义一个模板,并在 ContextMenu 模板中访问 ScrollViewer,然后在其上调用 ScrollToEnd()。

下面的示例演示了如何创建一个 WPF 上下文菜单,该菜单在启动时会滚动到末尾。您可以在 WPF NotifyIcon 中使用它。右键单击按钮以查看上下文菜单。

请注意,我已将其作为起点提供......您可以改进代码......减少一些重复......但它有效......例如......当菜单首先显示它不会滚动到最后我不得不捕获 SizeChanged 事件。

<Window x:Class="WpfApplication15.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="{x:Type ContextMenu}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContextMenu}">
<Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ScrollViewer x:Name="scrollviewer"
Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}"
CanContentScroll="True" >
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="{TemplateBinding Padding}"
KeyboardNavigation.DirectionalNavigation="Cycle"/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button x:Name="mybutton">
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="Test1"/>
</ContextMenu>
</Button.ContextMenu>
Test Button
</Button>
</Grid>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace WpfApplication15
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public static T FindVisualChild<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
return (T)child;
}

T childItem = FindVisualChild<T>(child);
if (childItem != null) return childItem;
}
}
return null;
}

public MainWindow()
{
InitializeComponent();

ContextMenu contextmenu = mybutton.ContextMenu;

// Add 100 more items

for (int i = 2; i <= 100; i++)
{
MenuItem mi = new MenuItem();
mi.Header = "Item" + i.ToString();

contextmenu.Items.Add(mi);
}

contextmenu.SizeChanged += new SizeChangedEventHandler(contextmenu_SizeChanged);
contextmenu.Loaded += new RoutedEventHandler(contextmenu_Loaded);

mybutton.ContextMenuOpening += new ContextMenuEventHandler(mybutton_ContextMenuOpening);
}

void contextmenu_SizeChanged(object sender, SizeChangedEventArgs e)
{
ContextMenu contextmenu = mybutton.ContextMenu;

ScrollViewer sv = FindVisualChild<ScrollViewer>(contextmenu);
if (sv != null)
{
sv.ScrollToEnd();
}
}

void contextmenu_Loaded(object sender, RoutedEventArgs e)
{
ContextMenu contextmenu = mybutton.ContextMenu;

ScrollViewer sv = FindVisualChild<ScrollViewer>(contextmenu);
if (sv != null)
{
sv.ScrollToEnd();
}
}

void mybutton_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
ContextMenu contextmenu = mybutton.ContextMenu;
ScrollViewer sv = FindVisualChild<ScrollViewer>(contextmenu);
if (sv != null)
{
sv.ScrollToEnd();
}
}
}
}

旁注....您可能想限制 ContextMenu 的高度...通过设置 MaxHeight...此处演示。

<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Button>
<Button.ContextMenu>
<ContextMenu MaxHeight="100">
<MenuItem Header="Test1"/>
<MenuItem Header="Test2"/>
<MenuItem Header="Test3"/>
<MenuItem Header="Test4"/>
<MenuItem Header="Test5"/>
<MenuItem Header="Test6"/>
<MenuItem Header="Test7"/>
<MenuItem Header="Test8"/>
<MenuItem Header="Test9"/>
</ContextMenu>
</Button.ContextMenu>
Test Button</Button>
</Grid>
</Page>

关于c# - 在 OverFlow 的情况下向下滚动 ContextMenu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12373383/

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