您可以做的是使用 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();
}
}
}
}
附带说明....您可能希望通过在此处设置 MaxHeight...demo 来限制 ContextMenu 的高度。
<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>