WPF菜单项标题文本部分隐藏
我对菜单中项目的显示方式进行了模板化,但由于未知原因,我无法在菜单项中显示整个文本。以下是问题的屏幕截图: 以下是我用来为其设置模板的标记代码:WPF菜单项标题文本部分隐藏,wpf,templates,menuitem,Wpf,Templates,Menuitem,我对菜单中项目的显示方式进行了模板化,但由于未知原因,我无法在菜单项中显示整个文本。以下是问题的屏幕截图: 以下是我用来为其设置模板的标记代码: <ItemsPanelTemplate x:Key="SideBarItemsPanelTemplate"> <StackPanel Orientation="Vertical"/> </ItemsPanelTemplate> <DataTemplate x:Key="SideBarItemTempl
<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}"/>
我搜索了很多,但没有任何帮助解决这个问题。希望我能在这里找到一些帮助
谢谢。你的行为越来越怪异,因为你在一个菜单项中有一个菜单项。通过在菜单上设置ItemTemplate,可以在每个菜单项上设置HeaderTemplate。MenuItem将呈现其普通模板,并且在通常放置标题文本的位置,它将有一个完整的其他MenuItem。我认为您看到的空间是为外部菜单项中的InputGestureText保留的空间 相反,您希望设置ItemContainerStyle。这将允许您在菜单创建的菜单项上设置属性。您需要使用一个技巧,以便为每个菜单项创建单独的图像对象。默认情况下,样式中包含的对象将被共享,每个菜单集将共享一个图像对象,但如果将它们放在单独的资源字典中,则可以将它们标记为未共享。请参阅和 大概是这样的:
<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>
你能试试菜单的宽度吗?emit会显示更多的文本,但这只是因为该项较长。右边还有一种剪贴。干得好!这很有效。但这让我意识到这种风格的行为会产生一种非常奇怪的副作用。由于这是一种与风格相关的行为,我似乎没有其他解决方案。我真的不理解这样一个事实,即我需要一个单独的资源来实现这一点,所以我只是在我当前的ResourceDictionary中添加了图像资源。