gpt4 book ai didi

c# - 重用 Menu.ItemContainerStyle

转载 作者:行者123 更新时间:2023-11-30 22:16:55 26 4
gpt4 key购买 nike

我创建了以下菜单。现在我想在不同的窗口上使用相同的菜单布局,并考虑将下面的代码转移到 Generic.xaml 并只在需要时引用它。最好的方法是什么,以便实际使用时间尽可能短?

<MenuHeight="27"VerticalAlignment="Top" ItemsSource="{Binding Menu}">
<Menu.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Command" Value="{Binding Execute}"/>
<Setter Property="Icon" Value="{Binding Icon}" />
<Setter Property="ToolTip" Value="{Binding ToolTip}"/>
</Style>
</Menu.ItemContainerStyle>
<Menu.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}" >
<TextBlock Text="{Binding Header}" />
<HierarchicalDataTemplate.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Command" Value="{Binding Execute}"/>
<Setter Property="Icon" Value="{Binding Icon}" />
<Setter Property="ToolTip" Value="{Binding ToolTip}"/>
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
</HierarchicalDataTemplate>
</Menu.ItemTemplate>
</Menu>

最佳答案

将它放在资源中,它将应用于范围内的每个菜单:

<Style x:Key="{x:Type Menu}" TargetType="Menu">
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="MenuItem">
<Setter Property="Command" Value="{Binding Execute}"/>
<Setter Property="Icon" Value="{Binding Icon}" />
<Setter Property="ToolTip" Value="{Binding ToolTip}"/>
</Style>
</Setter.Value>
</Setter>
<Setter Property="ItemTemplate">
<Setter.Value>
<HierarchicalDataTemplate ItemsSource="{Binding Children}" >
<TextBlock Text="{Binding Header}" />
<HierarchicalDataTemplate.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Command" Value="{Binding Execute}"/>
<Setter Property="Icon" Value="{Binding Icon}" />
<Setter Property="ToolTip" Value="{Binding ToolTip}"/>
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
</HierarchicalDataTemplate>
</Setter.Value>
</Setter>
</Style>

关于c# - 重用 Menu.ItemContainerStyle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17059616/

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