如何将手动创建的WPF Menuitem转换为模板/样式资源/控件模板

我有manualy创建了一个MenuItem.现在我希望它作为模板/样式资源/控制模板 – 无论这个任务最好的是什么.

我的MenuItem看起来像这样(我知道短代码):

<MenuItem
x:Name="Quit"                           << OUTSIDE TEMPLATE
Command="{Binding ShutdownCommand}">    << OUTSIDE TEMPLATE
<MenuItem.Header>
    <StackPanel 
        Orientation="Horizontal">
        <TextBlock 
            Width="150" 
            Text="Quit ERD Builder"/>   << OUTSIDE TEMPLATE
        <TextBlock 
            Width="80" 
            Margin="0,2,0,0" 
            TextAlignment="Right">
            <Border 
                Padding="4,0,4,0" 
                BorderBrush="#B0B0B0" 
                Background="#fff" 
                BorderThickness="1" 
                CornerRadius="6">
                <TextBlock 
                    Width="Auto" 
                    Text="Alt+F4"       << OUTSIDE TEMPLATE
                    FontSize="10" 
                    Foreground="#555" />
            </Border>
        </TextBlock>
    </StackPanel>
</MenuItem.Header>
<MenuItem.Icon>
    <Image 
        Width="16" 
        Height="16" 
        Margin="0,0,5,0" 
        HorizontalAlignment="Center" 
        VerticalAlignment="Center" 
        RenderOptions.BitmapScalingMode="HighQuality" 
        SnapsToDevicePixels="True">
        <Image.Source>
            <BitmapImage 
                UriSource="/ERDBuilder;component/icons/bw/102-walk.png" />  << OUTSIDE TEMPLATE
        </Image.Source>
    </Image>
</MenuItem.Icon>

我用<

<Style 
x:Key="TopTaskBarMenuitem" 
TargetType="MenuItem">
<Style.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="Background" Value="#ffff00" />    << DONT WORK
        <Setter Property="FontSize" Value="20" />           << WORKS
    </Trigger>
</Style.Triggers>
<Setter Property="Foreground" Value="#000" />               << WORKS
<Setter Property="BorderThickness" Value="1" />             << WORKS
<Setter Property="Width" Value="150"/>                      << WORKS

如果我手动“XAML”,这就是菜单的样子:

Manualy created Menuitem(我不允许在这里上传图片?!)

这是带有静态样式资源的Menuitem:

Menuitem with Style Resource

如您所见,“背景”颜色不会影响菜单项.

如果我希望我能在“菜单项目”中找到类似这样的东西 – 侧:

<MenuItem
Style="{StaticResource TopTaskBarMenuitem}"                     << TEMPLATE / STYLE BINDING
x:Name="Quit"                                                   << OUTSIDE TEMPLATE
Command="{Binding ShutdownCommand}"                             << OUTSIDE TEMPLATE
MyHeaderText="Quit ERD Builder"/>                               << OUTSIDE TEMPLATE
MyShortcutText="Alt+F4"                                         << OUTSIDE TEMPLATE
MyUriSource="/ERDBuilder;component/icons/bw/102-walk.png" />    << OUTSIDE TEMPLATE

非常感谢他们所有人的帮助!

PS:这里的所有三个代码发布都缺少最后一个代码行.我不知道为什么.我无法解决这个问题.

短剑

最佳答案 要获得这个,你必须创建自己的控件派生自MenuItem.

您需要做的就是使用DependencyProperties创建控件类以利用它的所有好处,阅读this以获取更多信息:

namespace MyControls
{
    class MyMenuItem : MenuItem
    {
        public string MyHeaderText
        {
            get { return (string)GetValue(MyHeaderTextProperty); }
            set { SetValue(MyHeaderTextProperty, value); }
        }
        public static readonly DependencyProperty MyHeaderTextProperty = DependencyProperty.Register("MyHeaderText", typeof(string), typeof(MyMenuItem));

        public string MyShortcutText
        {
            get { return (string)GetValue(MyShortcutTextProperty); }
            set { SetValue(MyShortcutTextProperty, value); }
        }
        public static readonly DependencyProperty MyShortcutTextProperty = DependencyProperty.Register("MyShortcutText", typeof(string), typeof(MyMenuItem));

        public string MyUriSource
        {
            get { return (string)GetValue(MyUriSourceProperty); }
            set { SetValue(MyUriSourceProperty, value); }
        }
        public static readonly DependencyProperty MyUriSourceProperty = DependencyProperty.Register("MyUriSource", typeof(string), typeof(MyMenuItem));
    }
}

现在您可以实例化您的控件,但您仍需要“重新模仿”它:

<mc:MyMenuItem MyHeaderText="Quit ERD Builder" MyShortcutText="Alt+F4" MyUriSource="/ERDBuilder;component/icons/bw/102-walk.png">
    <mc:MyMenuItem.Style>
        <Style TargetType="mc:MyMenuItem">
            <Style.Setters>
                <Setter Property="HeaderTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Width="150" Text="{Binding Mode=TwoWay, Path=MyHeaderText, RelativeSource={RelativeSource FindAncestor, AncestorType=mc:MyMenuItem}}"/>
                                <TextBlock Width="80" Margin="0,2,0,0" TextAlignment="Right">
                                    <Border Padding="4,0,4,0" BorderBrush="#B0B0B0" Background="#fff" BorderThickness="1" CornerRadius="6">
                                        <TextBlock Width="Auto" Text="{Binding Mode=TwoWay, Path=MyShortcutText, RelativeSource={RelativeSource FindAncestor, AncestorType=mc:MyMenuItem}}" FontSize="10" Foreground="#555" />
                                    </Border>
                                </TextBlock>
                            </StackPanel>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="Icon">
                    <Setter.Value>
                        <Image Width="16" Height="16" Margin="0,0,5,0" HorizontalAlignment="Center" VerticalAlignment="Center" RenderOptions.BitmapScalingMode="HighQuality" SnapsToDevicePixels="True" Source="{Binding Mode=OneWay, Path=MyUriSource, RelativeSource={RelativeSource FindAncestor, AncestorType=mc:MyMenuItem}}" />
                    </Setter.Value>
                </Setter>
            </Style.Setters>
        </Style>
    </mc:MyMenuItem.Style>
</mc:MyMenuItem>

不要忘记在窗口中(或者您可能放置此控件的任何位置)引用此新控件的命名空间:

xmlns:mc="clr-namespace:MyControls"

可以在ResourceDictionary中插入此样式,因此每次使用此控件时都不需要引用它.

<Style TargetType="mc:MyMenuItem">
    <!-- Style comes here -->
</Style>

然后你可以获得你的要求:

<mc:MyMenuItem MyHeaderText="Quit ERD Builder" MyShortcutText="Alt+F4" MyUriSource="/ERDBuilder;component/icons/bw/102-walk.png" />

我希望它可以帮到你!

点赞