当我们在ContextMenu上绑定了集合,会对应的生成ContextMenuItem,我们怎么通过定义点击事件MenuItem.Click去拿到各个MenuItem对应绑定的类实例呢?比如ContextMenu的ItemsSource为Collection<User>,他的每一个MenuItem对应一个User实例,我们可以用以下代码取得:
private void ContextMenuItemClick(object sender, RoutedEventArgs e) { MenuItem menuItem = e.OriginalSource as MenuItem; User user = menuItem.Header as User; }