2012-01-11 150 views
2

我要走這個很好的例子,我發現,它的工作原理,但是當我有一個子菜單分支關閉其中一個菜單項時,樣式不會在該子菜單上呈現。建立一個透明的WPF菜單和子菜單

WPF Transparent menu

我試圖複製什麼是設置了MenuItem.TopLevelHeaderTemplateKey以下作爲目標MenuItem.SubmenuHeaderTemplateKey另一個控件模板,這部作品在某種程度上,但子菜單的根不在同一位置的其他沒有子菜單的菜單項。

有沒有一種方法可以編輯ControlTemplate,告訴每個菜單項是否是根或孩子的外觀和行爲相同?

ETA:這裏是我的工作xaml,項目本身被推到左邊的菜單項。我無法解決原因。

<ControlTemplate x:Key="{x:Static MenuItem.TopLevelHeaderTemplateKey}" TargetType="{x:Type MenuItem}"> 
    <Border Name="Border" > 
    <Grid> 
    <ContentPresenter 
     Margin="6,3,6,3" 
     ContentSource="Header"        
     RecognizesAccessKey="True" /> 
    <Popup 
     Name="Popup" 
     Placement="Bottom" 
     IsOpen="{TemplateBinding IsSubmenuOpen}" 
     AllowsTransparency="True" 
     Focusable="False"       
     PopupAnimation="Fade"> 
     <Border 
     Name="SubmenuBorder" 
     SnapsToDevicePixels="True" 

     Background="#9B000000"> 
          <StackPanel 
      IsItemsHost="True" 
      KeyboardNavigation.DirectionalNavigation="Cycle" /> 
     </Border> 
    </Popup> 
    </Grid> 
    </Border> 
      <ControlTemplate.Triggers> 
       <Trigger Property="IsSuspendingPopupAnimation" Value="true"> 
        <Setter TargetName="Popup" Property="PopupAnimation" Value="None"/> 
       </Trigger> 
       <Trigger Property="IsHighlighted" Value="true"> 
        <Setter TargetName="Border" Property="Background" Value="#C0C0C0"/> 
        <Setter TargetName="Border" Property="BorderBrush" Value="Transparent"/> 
       </Trigger> 
       <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="True"> 
        <Setter TargetName="SubmenuBorder" Property="CornerRadius" Value="0,0,4,4"/> 
        <Setter TargetName="SubmenuBorder" Property="Padding" Value="0,0,0,3"/> 
       </Trigger> 
       <Trigger Property="IsEnabled" Value="False"> 
        <Setter Property="Foreground" Value="#888888"/> 
       </Trigger> 
      </ControlTemplate.Triggers> 
    </ControlTemplate> 

     <ControlTemplate x:Key="{x:Static MenuItem.SubmenuHeaderTemplateKey}" TargetType="{x:Type MenuItem}"> 
      <Border Name="Border" > 
       <Grid> 
     <ContentPresenter 
     Margin="6,3,6,3" 
     ContentSource="Header"        
     RecognizesAccessKey="True" /> 
        <Popup 
     Name="Popup" 
     Placement="Right" 
     IsOpen="{TemplateBinding IsSubmenuOpen}" 
     AllowsTransparency="True" 
     Focusable="False"       
     PopupAnimation="Fade"> 
         <Border 
     Name="SubmenuBorder" 
     SnapsToDevicePixels="True" 
     Background="#9B000000"> 
          <StackPanel 
      IsItemsHost="True" 
      KeyboardNavigation.DirectionalNavigation="Cycle" /> 
         </Border> 
        </Popup> 
       </Grid> 
      </Border> 
      <ControlTemplate.Triggers> 
       <Trigger Property="IsSuspendingPopupAnimation" Value="true"> 
        <Setter TargetName="Popup" Property="PopupAnimation" Value="None"/> 
       </Trigger> 
       <Trigger Property="IsHighlighted" Value="true"> 
        <Setter TargetName="Border" Property="Background" Value="#C0C0C0"/> 
        <Setter TargetName="Border" Property="BorderBrush" Value="Transparent"/> 
       </Trigger> 
       <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="True"> 
        <Setter TargetName="SubmenuBorder" Property="CornerRadius" Value="0,0,4,4"/> 
        <Setter TargetName="SubmenuBorder" Property="Padding" Value="0,0,0,3"/> 
       </Trigger> 
       <Trigger Property="IsEnabled" Value="False"> 
        <Setter Property="Foreground" Value="#888888"/> 
       </Trigger> 
      </ControlTemplate.Triggers> 
     </ControlTemplate> 
+0

? – jcvegan 2012-01-11 23:10:18

+0

不,我沒有混合,我可以使用一個月的免費版本,但我們有專業的VS,所以我們沒有得到混合副本。 – 2012-01-12 15:31:57

+0

編輯以顯示我正在工作的xaml,所有菜單項都是透明的,就像我想要的一樣,但是如果它是一個有孩子的項目,它將被推到左邊。 – 2012-01-12 20:36:06

回答

0

我建議您首先採取一個完整的MenuItem模板示例,並對其進行修改以使其變爲半透明。您可以在這裏的msdn網站上找到MenuItem的完整示例代碼:http://msdn.microsoft.com/en-us/library/ms747082(v=VS.90).aspx

此代碼包括頂級標題,頂級項目,子菜單標題和子菜單項的單獨模板。然後使用觸發器在MenuItem樣式中包含所有模板。

我認爲你最感興趣的代碼是子菜單頭模板。通過這段代碼來看看如何使子菜單的根看起來與沒有子菜單的菜單項相同。讓我知道你是否仍然有麻煩。

+0

是的,我無法弄清楚。仔細閱讀代碼,嘗試解讀我需要更改的內容,讓它們看起來相同,但無濟於事。我正在使用的xaml編輯並張貼在上面。 – 2012-01-12 20:51:40