2010-07-28 172 views
0

我爲項目在菜單中顯示的方式進行了模板化,但由於未知原因,我無法在MenuItem中顯示整個文本。這是問題的一個屏幕截圖: alt text http://img203.imageshack.us/img203/4513/capturexz.pngWPF MenuItem標題文本部分隱藏

下面是我用模板它的標記代碼:

<ItemsPanelTemplate x:Key="SideBarItemsPanelTemplate"> 
    <StackPanel Orientation="Vertical"/> 
</ItemsPanelTemplate> 
<DataTemplate x:Key="SideBarItemTemplate"> 
    <MenuItem Command="{Binding}" Header="{Binding Text}" Background="AliceBlue"> 
     <MenuItem.Icon> 
      <Image Width="16" Height="16" Source="{Binding Image}"/> 
     </MenuItem.Icon> 
    </MenuItem> 
</DataTemplate> 
<Style x:Key="SideBarStyle" TargetType="{x:Type Menu}"> 
    <Setter Property="ItemTemplate" Value="{StaticResource SideBarItemTemplate}"/> 
    <Setter Property="ItemsPanel" Value="{StaticResource SideBarItemsPanelTemplate}"/> 
    <Setter Property="Background" Value="White"/> 
</Style> 

,並顯示它:

<Menu ItemsSource="{Binding Commands}" Style="{StaticResource SideBarStyle}"/> 

我搜索了很多,但沒有什麼幫助解決這個問題。希望我會在這裏找到一些幫助。

謝謝。

+0

你可以嘗試寬度爲菜單項 – Ragunathan 2010-07-28 10:09:20

+0

它顯示了我更多的文字一點點,但,這只是因爲項目更長。右側仍然有一種裁剪方式。 – Ucodia 2010-07-28 10:18:31

+0

嘗試寬度到菜單 – Ragunathan 2010-07-28 11:42:33

回答

1

由於MenuItem中有一個MenuItem,因此會出現奇怪的行爲。通過在菜單上設置ItemTemplate,您可以在每個MenuItem上設置HeaderTemplate。 MenuItem將呈現其普通模板,並且標題文本通常將放置在其中的位置將包含整個其他MenuItem。我認爲你看到的空間是爲外部MenuItem中的InputGestureText保留的空間。

而是想要設置ItemContainerStyle。這會讓你在菜單創建的MenuItems上設置屬性。您需要使用一種技巧,以便您可以爲每個MenuItem創建一個單獨的Image對象。默認情況下,Style中包含的對象將被共享,並且您將獲得每個MenuIte共享的一個Image對象,但是如果將它們放在單獨的資源字典中,則可以將它們標記爲不共享。見this issue on Connectlinked workaround

事情是這樣的:

<Style x:Key="SideBarStyle" TargetType="{x:Type Menu}"> 
    <Setter Property="ItemsPanel" Value="{StaticResource SideBarItemsPanelTemplate}"/> 
    <Setter Property="Background" Value="White"/> 
    <Setter Property="ItemContainerStyle"> 
     <Setter.Value> 
      <Style TargetType="MenuItem"> 
       <Style.Resources> 
        <ResourceDictionary Source="Icon.xaml"/> 
       </Style.Resources> 
       <Setter Property="Command" Value="{Binding}"/> 
       <Setter Property="Header" Value="{Binding Text}"/> 
       <Setter Property="Background" Value="AliceBlue"/> 
       <Setter Property="Icon" Value="{StaticResource Icon}"/> 
      </Style> 
     </Setter.Value> 
    </Setter> 
</Style> 

其中Icon.xaml包含:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Image x:Key="Icon" x:Shared="False" Width="16" Height="16" Source="{Binding Image}"/> 
</ResourceDictionary> 
+0

幹得好!這工作得很好。但它讓我意識到這種風格行爲會導致一種非常奇怪的副作用。由於這是與Style有關的行爲,因此我似乎沒有其他解決方案。我並不十分欣賞這樣一個事實,即我將需要一個獨立的資源,因此我只在當前的ResourceDictionary中添加了Image資源。 – Ucodia 2010-07-28 13:41:03