2013-05-25 63 views
3

如果我有一個命令列表,其中每個命令都有一個組號,那麼如何在contextmenu中顯示這些命令?在上下文菜單中分組

下面是我嘗試過的代碼,但我似乎無法得到子菜單項顯示。

namespace groupTest 
{ 
    public class GroupedCommand 
    { 
     public string Name { get; set; } 
     public ICommand Command { get; set; } 
     public int Group { get; set; } 
    } 

    public partial class Window1 : Window 
    { 
     public List<GroupedCommand> Commands 
     { 
      get; 
      private set; 
     } 

     public Window1() 
     { 
      Commands = new List<GroupedCommand>(); 
      Commands.Add(new GroupedCommand() { Name = "A", Group = 0 }); 
      Commands.Add(new GroupedCommand() { Name = "B", Group = 0 }); 
      Commands.Add(new GroupedCommand() { Name = "C", Group = 1 }); 
      Commands.Add(new GroupedCommand() { Name = "D", Group = 1 }); 

      var defView = (CollectionView)CollectionViewSource.GetDefaultView(Commands); 
      defView.GroupDescriptions.Add(new PropertyGroupDescription("Group")); 

      DataContext = this; 
      InitializeComponent(); 
     } 
    } 
} 

<Window x:Class="groupTest.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300"> 

    <Window.ContextMenu> 
     <ContextMenu ItemsSource="{Binding Commands}"> 
      <ContextMenu.GroupStyle> 
       <GroupStyle> 
        <GroupStyle.ContainerStyle> 
         <Style TargetType="{x:Type GroupItem}"> 
          <Setter Property="Template"> 
           <Setter.Value> 
            <ControlTemplate TargetType="{x:Type GroupItem}"> 
             <MenuItem Header="{Binding}"> 
              <ItemsPresenter /> 
             </MenuItem> 
            </ControlTemplate> 
           </Setter.Value> 
          </Setter> 
         </Style> 
        </GroupStyle.ContainerStyle> 
       </GroupStyle> 
      </ContextMenu.GroupStyle> 
     </ContextMenu> 
    </Window.ContextMenu> 
</Window> 

回答

0

我設法通過更改控制模板GroupItem和附加的行爲來實現此目的。

我已經取代了以下ControlTemplate

<ControlTemplate TargetType="{x:Type GroupItem}"> 
    <MenuItem Header="{Binding}"> 
     <ItemsPresenter /> 
    </MenuItem> 
</ControlTemplate> 

<ControlTemplate TargetType="{x:Type GroupItem}"> 
    <MenuItem Header="{Binding}" ItemsSource="{Binding Items}" Style="{StaticResource GroupMenuItemStyle}" /> 
</ControlTemplate> 

而且,當時增加了一個附加的行爲顯示/隱藏子菜單項,當鼠標進入/離開MenuItem

public static class GroupMenuBehavior 
    { 
     public static readonly DependencyProperty DummyProperty = 
      DependencyProperty.RegisterAttached("Dummy", typeof (bool), typeof (GroupMenuBehavior), new PropertyMetadata(default(bool), PropertyChangedCallback)); 

     private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) 
     { 
      var mi = dependencyObject as MenuItem; 
      if (mi == null) return; 
      if (mi.IsLoaded) 
      { 
       AddHandlers(mi); 
      } 
      else 
      { 
       mi.Loaded += MiOnLoaded; 
      } 
     } 

     private static void MiOnLoaded(object sender, RoutedEventArgs routedEventArgs) 
     { 
      var mi = (MenuItem) sender; 
      mi.Loaded -= MiOnLoaded; 
      AddHandlers(mi); 
     } 

     static void AddHandlers(MenuItem mi) 
     { 
      mi.MouseEnter += MiOnMouseEnter; 
      mi.MouseLeave += MiOnMouseLeave; 
     } 

     private static void MiOnMouseLeave(object sender, MouseEventArgs mouseEventArgs) 
     { 
      var mi = (MenuItem)sender; 
      if (mi.IsSubmenuOpen) 
       mi.IsSubmenuOpen = false; 
     } 

     private static void MiOnMouseEnter(object sender, MouseEventArgs mouseEventArgs) 
     { 
      var mi = (MenuItem) sender; 
      if (mi.Items != null && mi.Items.Count != 0) 
       mi.IsSubmenuOpen = true; 
     } 

     public static void SetDummy(MenuItem menuItem, bool value) 
     { 
      menuItem.SetValue(DummyProperty, value); 
     } 

     public static bool GetDummy(MenuItem menuItem) 
     { 
      return (bool) menuItem.GetValue(DummyProperty); 
     } 
    } 

而且,MenuItem附加上述行爲的樣式或:

<Style TargetType="{x:Type MenuItem}" x:Key="GroupMenuItemStyle"> 
    <Setter Property="local:GroupMenuBehavior.Dummy" Value="True" /> 
</Style> 

我知道,這是一個醜陋的黑客/修復。但是,無法通過其他方式使其工作。