gpt4 book ai didi

wpf - HierarchicalDataTemplate.ItemTemplate 中的水平拉伸(stretch) WPF ContextMenu MenuItem

转载 作者:行者123 更新时间:2023-12-03 10:12:39 25 4
gpt4 key购买 nike

我有一个 WPF 系统托盘栏应用程序。右键单击托盘栏图标时,会出现 ContextMenu使用 HierarchicalDataTemplate获得 2 级动态填充菜单。它可以工作,但是第二级项目的“可点击”部分没有正确拉伸(stretch)到父控件的可用宽度。而是参见图片:

Picture of menu where 2nd level items dont fill the parent control

现在用户必须单击 MenuItem 的较暗部分(文本所在的位置)执行Command这个项目的。我希望整个菜单行能够触发 Command .

这是我的 XAML:

<CollectionViewSource x:Key="Items" Source="{Binding Path=Items}" />
<ContextMenu x:Shared="false" x:Key="Menu" HorizontalContentAlignment="Stretch">
<ContextMenu.ItemTemplate>
<HierarchicalDataTemplate DataType="SystemTrayItemsViewModel" ItemsSource="{Binding Items}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Converter={StaticResource TabIconConverter}}" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<MenuItem Header="{Binding Text}" ToolTip="{Binding ToolTip}" Command="{Binding ToClipBoardCommand}" HorizontalContentAlignment="Stretch" />
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</ContextMenu.ItemTemplate>
<ContextMenu.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Source={StaticResource Items}}">
</CollectionContainer>
<Separator />
<MenuItem Header="Exit" cal:Message.Attach="ExitApplication" />
</CompositeCollection>
</ContextMenu.ItemsSource>
</ContextMenu>

如需完整源代码,请查看 https://github.com/kasperhlund/textgrunt

最佳答案

我刚刚在 another SO question 中找到了答案.
似乎问题在于指定 MenuItemItemTemplate 内. ContextMenu显然是在包装另一个 MenuItem围绕ItemTemplate ,导致这种嵌套效应。相反,您必须通过 MenuItem 执行此操作的Style :

<ContextMenu>
<ContextMenu.Resources>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Header" Value="{Binding Text}"/>
<Setter Property="ToolTip" Value="{Binding ToolTip}"/>
<Setter Property="Command" Value="{Binding ToClipBoardCommand}"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ContextMenu.Resources>
</ContextMenu>

关于wpf - HierarchicalDataTemplate.ItemTemplate 中的水平拉伸(stretch) WPF ContextMenu MenuItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48597371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com