我想在用户单击按钮
时显示下拉菜单
。类似 comboBox
但它不是组合框,而是一个按钮。我该怎么做??
最佳答案
我使用 PopupMenu 解决了这个问题。这是代码供其他人引用。
public static Rect GetElementRect(FrameworkElement element)
{
GeneralTransform buttonTransform = element.TransformToVisual(null);
Point point = buttonTransform.TransformPoint(new Point());
return new Rect(point, new Size(element.ActualWidth, element.ActualHeight));
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
var menu = new PopupMenu();
menu.Commands.Add(new UICommand("Label", (command) =>
{
//do work
}));
// We don't want to obscure content, so pass in a rectangle representing the sender of the context menu event.
// We registered command callbacks; no need to handle the menu completion event
var chosenCommand = await menu.ShowForSelectionAsync(GetElementRect((FrameworkElement)sender));
if (chosenCommand == null) // The command is null if no command was invoked.
{
}
}
关于button - 单击按钮时添加下拉菜单 - Windows 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13455273/