通过样式设置器设置 MenuItem 图标

发布于 2024-10-20 08:40:20 字数 597 浏览 1 评论 0原文

<Style x:Key="ContextMenuItemStyle" TargetType="{x:Type MenuItem}">
    <Setter Property="Icon" Value="{Binding Icon}" />
    <Setter Property="Header" Value="{Binding Text}" />
    <Setter Property="ItemsSource" Value="{Binding Children}" />
    <Setter Property="Command" Value="{Binding Command}" />
</Style>

在这样的代码中设置它:

Uri refreshUri = new Uri("..\\Resources\\Refresh16.bmp",UriKind.Relative);
BitmapImage refreshIcon = new BitmapImage();
refreshIcon.UriSource = refreshUri;

图标没有显示,有任何线索吗?

<Style x:Key="ContextMenuItemStyle" TargetType="{x:Type MenuItem}">
    <Setter Property="Icon" Value="{Binding Icon}" />
    <Setter Property="Header" Value="{Binding Text}" />
    <Setter Property="ItemsSource" Value="{Binding Children}" />
    <Setter Property="Command" Value="{Binding Command}" />
</Style>

setting it in code like this:

Uri refreshUri = new Uri("..\\Resources\\Refresh16.bmp",UriKind.Relative);
BitmapImage refreshIcon = new BitmapImage();
refreshIcon.UriSource = refreshUri;

the Icon doesn't show up, any clues ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

つ低調成傷 2024-10-27 08:40:20

如果 refreshIconIcon 属性的来源,那么您可能需要在代码示例之后调用 NotifyPropertyChanged("Icon") (并实现 INotifyPropertyChanged 接口)和/或将 Icon 声明为 DependencyProperty

以下是有关 INotifyPropertyChanged 界面。

啊,我明白你的问题...尝试将 Icon 属性设置为 Image 并绑定到 Image 的源:

<Setter Property="Icon">
    <Setter.Value>
        <Image Source="{Binding Icon}" />
    </Setter.Value>
</Setter>

您也可以只需将图像放入主项目中的 Images 文件夹中,然后在 xaml 中引用它,如下所示:

<Setter Property="Icon">
    <Setter.Value>
        <Image Source="/ProjectName;component/Images/IconName.ico" />
    </Setter.Value>
</Setter>

If the refreshIcon is the source of your Icon property, then you may need to either call NotifyPropertyChanged("Icon") after your code example (and implement the INotifyPropertyChanged interface) and/or declare Icon as a DependencyProperty.

Here is a link to more information about the INotifyPropertyChanged interface.

Ahh, I see your problem... try setting the Icon property to an Image and bind to the source of the Image:

<Setter Property="Icon">
    <Setter.Value>
        <Image Source="{Binding Icon}" />
    </Setter.Value>
</Setter>

You can also just put the image into an Images folder in your main project and reference it in xaml like this:

<Setter Property="Icon">
    <Setter.Value>
        <Image Source="/ProjectName;component/Images/IconName.ico" />
    </Setter.Value>
</Setter>
装迷糊 2024-10-27 08:40:20

对于仍在寻找解决方案的人来说,这对我有用:

<Window.Resources>
    <Image x:Key="Icon" Source="/ProjectName;component/Images/IconName.ico" x:Shared="false"/>
    <Style x:Key="MenuItem">
        <Setter Property="MenuItem.Header" Value="Header Text"/>
        <Setter Property="MenuItem.Icon" Value="{DynamicResource Icon}"/>
    </Style>
</Window.Resources>

For anyone still looking for a solution, this worked for me:

<Window.Resources>
    <Image x:Key="Icon" Source="/ProjectName;component/Images/IconName.ico" x:Shared="false"/>
    <Style x:Key="MenuItem">
        <Setter Property="MenuItem.Header" Value="Header Text"/>
        <Setter Property="MenuItem.Icon" Value="{DynamicResource Icon}"/>
    </Style>
</Window.Resources>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文