2013-06-04 29 views
0

我喜歡對子級有一個菜單如下所示:動態和靜態的MenuItems,無論是在主菜單,文本菜單

┌──────────────────────────┐ 
│ MenuItem always the same │ 
│ <Separator />   │ 
│ MenuItem read from XML 1 │ 
│ MenuItem read from XML 2 │ 
│ MenuItem read from XML n │ 
└──────────────────────────┘ 

,這應該在主菜單,爲文本菜單還的子菜單中重複使用。

我現在有在XAML如下:

<Window.Resources> 
    <XmlDataProvider x:Key="ItemTypes" Source="C:\Config.xml" 
     XPath="Configuration/ItemTypes/ItemType" /> 

    <collections:ArrayList x:Key="mnuAdd" x:Shared="False"> 
     <MenuItem Command="local:MainWindow.AddItemGroup" Header="Item Group" /> 
     <Separator /> 
     <ItemsControl ItemsSource="{Binding Source={StaticResource ItemTypes}}" /> 
    </collections:ArrayList> 

</Window.Resources> 

而在XML中的以下內容:

<Configuration> 
    <ItemTypes> 
     <ItemTypeName="Item1" DisplayName="First Item" /> 
     <ItemTypeName="Item2" DisplayName="Second Item" /> 
    </ItemTypes> 
</Configuration> 

當然,我有HierarchicalDataTemplate,但它永遠不會顯示正確。

如果我將其設置爲這一個:

<HierarchicalDataTemplate DataType="ItemType"> 
     <TextBlock Text="{Binding [email protected], StringFormat={}{0}}" 
      Tag="{Binding [email protected]}" MouseLeftButtonUp="AddItemMenu_Click" 
      MouseRightButtonUp="AddItemMenu_Click" /> 
    </HierarchicalDataTemplate> 

它顯示是這樣的:

enter image description here

如果我將其設置爲這一個:

<HierarchicalDataTemplate DataType="ItemType"> 
     <MenuItem Header="{Binding [email protected], StringFormat={}{0}}" 
      Tag="{Binding [email protected]}" MouseLeftButtonUp="AddItemMenu_Click" 
      MouseRightButtonUp="AddItemMenu_Click" /> 
    </HierarchicalDataTemplate> 

它顯示這樣:

enter image description here

我該如何將它設置爲正常顯示正常的菜單項?

請不,根據以往的研究,爲<collections:ArrayList x:Key="mnuAdd" x:Shared="False">是強制性的,因爲我想無論是在主菜單,文本菜單中所顯示的相同的靜態資源。如果我不使用它,其中一個消失。

UPDATE 1:

根據H.B.改變了<collections:ArrayList>CompositeCollection

<CompositeCollection x:Key="mnuAdd" x:Shared="False"> 
     <MenuItem Command="local:MainWindow.AddItemGroup" Header="Item Group" /> 
     <Separator /> 
     <CollectionContainer Collection="{Binding Source={StaticResource ItemTypes}}" /> 
    </CompositeCollection> 

現在,如果我的DataTemplate是MenuItem,它仍然顯示尷尬:

enter image description here

如果我的DataTemplate是TextBlock,它顯示正常,但點擊次數會只處理如果是文本上的,則不是該文本稍微離開。

enter image description here

我怎樣才能使它發揮作用好不好看?

編輯2: 通過以下水平延伸的TextBlock好不容易纔解決了這個問題點擊:

<DataTemplate DataType="ItemType"> 
     <TextBlock Text="{Binding [email protected], StringFormat={}{0}}" 
      Tag="{Binding [email protected]}" MouseLeftButtonUp="AddItemMenu_Click" 
      MouseRightButtonUp="AddTestItemMenu_Click" 
      Margin="-30,0,-60,0" Padding="30,0,60,0" HorizontalAlignment="Stretch" /> 
    </DataTemplate> 

現在看起來不錯,效果很好。

+0

您應該使靜態資源不共享,而不是... –

+0

它究竟如何解決顯示問題? –

+0

好吧,實際上你可以通過不把任何UI元素放入你的列表中,並讓'Menu' /'ContextMenu'處理UI的創建來解決它,但我想在這裏使用'x:Shared'是可行的。 –

回答

3

使用CompositeCollection建立您的列表,使用CollectionContainer作爲動態部分。

+0

我開始使用這種方法。有了這個,發生以下情況:如果我打開MainMenu,項目顯示。然後,如果我打開ContextMenu,則會顯示項目。從這一刻起,這些項目將被綁定到ContextMenu,並且每當我再次打開MainMenu時,項目將不會顯示。這就是我在問題結尾處用斜體文字表示的內容。 –

+0

@ user2270404:我沒有告訴你在我的答案中放入'x:Shared',這是一個可以以某種方式處理的不同問題。 –

+0

我使用集合:ArrayList或CompositeCollection。 CompositeCollection不能有x:Shared。 –