2010-07-28 170 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