以下类派生自System.Windows.Controls.UserControl
. 在所述类中,我调用OpenFileDialog
以打开 XAML 文件(工作流文件)。接下来,我在鼠标右键单击时实现了一个动态菜单。菜单不显示。这是线程问题还是 UI 问题?在我的研究中,我一直无法找到解决方案。
提前致谢。
private void File_Open_Click(object sender, RoutedEventArgs e)
{
var fileDialog = new OpenFileDialog();
fileDialog.Title = "Open Workflow";
fileDialog.Filter = "Workflow| *.xaml";
if (fileDialog.ShowDialog() == DialogResult.OK)
{
LoadWorkflow(fileDialog.FileName);
MouseDown += new System.Windows.Input.MouseButtonEventHandler(mouseClickedResponse);
}
}
private void mouseClickedResponse(object sender, System.Windows.Input.MouseEventArgs e)
{
if (e.RightButton == MouseButtonState.Pressed)
{
LoadMenuItems();
}
}
private void LoadMenuItems()
{
System.Windows.Controls.ContextMenu contextmenu = new System.Windows.Controls.ContextMenu();
System.Windows.Controls.MenuItem item1 = new System.Windows.Controls.MenuItem();
item1.Header = "A new Test";
contextmenu.Items.Add(item1);
this.ContextMenu = contextmenu;
this.ContextMenu.Visibility = Visibility.Visible;
}