C# 上下文菜单不支持';显式打开时不显示绑定项(通过ContextMenu.IsOpen)
我有以下代码: XAML:C# 上下文菜单不支持';显式打开时不显示绑定项(通过ContextMenu.IsOpen),c#,.net,wpf,C#,.net,Wpf,我有以下代码: XAML: <Window x:Class="ContextMenuIssue.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 x:Class="ContextMenuIssue.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">
<Grid>
<TextBox MouseRightButtonUp="TextBox_OnMouseRightButtonUp">
<TextBox.ContextMenu>
<ContextMenu ItemsSource="{Binding Items}"></ContextMenu>
</TextBox.ContextMenu>
</TextBox>
</Grid>
</Window>
namespace ContextMenuIssue
{
class Model
{
public Model()
{
_items = new List<string> {"A", "B", "C"};
}
private List<string> _items;
public List<string> Items
{
get { return _items; }
set { _items = value; }
}
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new Model();
}
private void TextBox_OnMouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
//TextBox textBox = ((TextBox) sender);
//textBox.ContextMenu.IsOpen = true;
//e.Handled = true;
}
}
}
代码隐藏:
<Window x:Class="ContextMenuIssue.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">
<Grid>
<TextBox MouseRightButtonUp="TextBox_OnMouseRightButtonUp">
<TextBox.ContextMenu>
<ContextMenu ItemsSource="{Binding Items}"></ContextMenu>
</TextBox.ContextMenu>
</TextBox>
</Grid>
</Window>
namespace ContextMenuIssue
{
class Model
{
public Model()
{
_items = new List<string> {"A", "B", "C"};
}
private List<string> _items;
public List<string> Items
{
get { return _items; }
set { _items = value; }
}
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new Model();
}
private void TextBox_OnMouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
//TextBox textBox = ((TextBox) sender);
//textBox.ContextMenu.IsOpen = true;
//e.Handled = true;
}
}
}
namespace ContextMenuIssue
{
类模型
{
公共模型()
{
_项目=新列表{“A”、“B”、“C”};
}
私人物品清单;
公共清单项目
{
获取{return\u items;}
设置{u items=value;}
}
}
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
this.DataContext=新模型();
}
私有void文本框\u OnMouseRightButtonUp(对象发送器,鼠标按钮ventargs e)
{
//TextBox TextBox=((TextBox)发送方);
//textBox.ContextMenu.IsOpen=true;
//e、 已处理=正确;
}
}
}
当我运行它并右键单击文本框时,我得到一个上下文菜单,正如预期的那样,有3个项目(a、B和C)。但是,如果我试图明确地打开上下文菜单,通过注释mouseRightButtonup上的TextBox\u中的代码,我会得到一个空的上下文菜单
当我试图调试它时,上下文菜单对象似乎存在,但它是空的且未初始化,例如,它的ItemsSource属性设置为null
有人知道为什么会发生这种情况吗?ContextMenu不属于元素树,因此,DataContext属性无法传播到ContextMenu。要解决此问题,请在OnMouseRightButtonUp中手动设置ContextMenu的数据上下文:
textBox.ContextMenu.DataContext=this.DataContext 可能在右键单击的默认处理中,WPF将DataContext分配给上下文菜单。在我的代码中,当手动打开conext菜单时,我执行以下操作:this.ContextMenu.DataContext=this.DataContext。菜单按预期运行。错误。你是对的!但是我很困惑。当我隐式打开上下文菜单时,为什么它工作得很好?没有这个活动?DataContext不应该有同样的问题吗?很难说它为什么工作,可能正如Jogy提到的,WPF将DataContext附加到了上下文菜单。快速测试表明,在ContextMenuOpening事件中,上下文菜单的DataContext为null,但在ContextMenuOpened事件中,DataContext设置为视图模型(当“隐式”打开上下文菜单时)。