2016-02-27 51 views
2

我正在使用MahApps Metro UI工具包編寫WPF應用程序。通過ContextMenu使用MahApps圖標

http://mahapps.com/guides/quick-start.html

從他們的網站上的指導,我已經基本完成我的應用程序,它看起來光滑。我唯一的問題是我一直無法知道如何使用它們提供的圖標包作爲contextmenu圖標。

enter image description here

這裏是什麼,我試圖做一個直觀的例子。雖然我能夠獲得原始的「Windows」菜單項以顯示它的圖標,但我無法對contextmenu菜單項執行相同的操作。有什麼我做錯了或解決這個問題的方法嗎?

這裏是我的.xaml:

<Menu IsMainMenu="True"> 
    <MenuItem Header="_Windows" ContextMenuService.IsEnabled="False" Click="WindowsMenuItem_Click"> 
     <MenuItem.Icon> 
      <Rectangle Width="15" Height="15" Fill="{Binding RelativeSource={RelativeSource AncestorType=MenuItem}, Path=Foreground}"> 
       <Rectangle.OpacityMask> 
        <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_window}" /> 
       </Rectangle.OpacityMask> 
      </Rectangle> 
     </MenuItem.Icon> 
     <MenuItem.ContextMenu> 
      <ContextMenu> 
       <MenuItem Header="_Welcome Module"> 
        <MenuItem.Icon> 
         <Rectangle Width="15" Height="15" Fill="{Binding RelativeSource={RelativeSource AncestorType=MenuItem}, Path=Foreground}"> 
          <Rectangle.OpacityMask> 
           <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_home}" /> 
          </Rectangle.OpacityMask> 
         </Rectangle> 
        </MenuItem.Icon> 
       </MenuItem> 
       <MenuItem Header="_Schedule Module"> 
        <MenuItem.Icon> 
         <Rectangle Width="15" Height="15" Fill="{Binding RelativeSource={RelativeSource AncestorType=MenuItem}, Path=Foreground}"> 
          <Rectangle.OpacityMask> 
           <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_calendar}" /> 
          </Rectangle.OpacityMask> 
         </Rectangle> 
        </MenuItem.Icon> 
       </MenuItem> 
       <MenuItem Header="_Performance Module"> 
        <MenuItem.Icon> 
         <Rectangle Width="15" Height="15" Fill="{Binding RelativeSource={RelativeSource AncestorType=MenuItem}, Path=Foreground}"> 
          <Rectangle.OpacityMask> 
           <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_table}" /> 
          </Rectangle.OpacityMask> 
         </Rectangle> 
        </MenuItem.Icon> 
       </MenuItem> 
       <MenuItem Header="_Audit Module"> 
        <MenuItem.Icon> 
         <Rectangle Width="15" Height="15" Fill="{Binding RelativeSource={RelativeSource AncestorType=MenuItem}, Path=Foreground}"> 
          <Rectangle.OpacityMask> 
           <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_graph_line}" /> 
          </Rectangle.OpacityMask> 
         </Rectangle> 
        </MenuItem.Icon> 
       </MenuItem> 
      </ContextMenu> 
     </MenuItem.ContextMenu> 
    </MenuItem> 
</Menu> 

而且我.xaml.cs:

public partial class MainWindow 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     DataContext = new MainWindowViewModel(); 
    } 

    private void WindowsMenuItem_Click(object sender, RoutedEventArgs e) 
    { 
     (sender as MenuItem).ContextMenu.IsEnabled = true; 
     (sender as MenuItem).ContextMenu.PlacementTarget = (sender as MenuItem); 
     (sender as MenuItem).ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom; 
     (sender as MenuItem).ContextMenu.IsOpen = true; 
    } 
} 

回答

5

我得到了它在我的應用程序運行使用:

<Window.Resources> 
    <ResourceDictionary> 
     <VisualBrush x:Key="RunAllTestsIcon" Visual="{StaticResource appbar_list_check}"/> 
    </ResourceDictionary> 
</Window.Resources> 
... 
<ContextMenu StaysOpen="True"> 
    <MenuItem Header="RunAllTests"> 
     <MenuItem.Icon> 
     <Rectangle Width="22" Height="22" Fill="{StaticResource RunAllTestsIcon}"/> 
     </MenuItem.Icon> 
    </MenuItem> 
</ContextMenu>