无法通过设置器设置 System.Windows.Controls.MenuItem.Icon

发布于 2024-08-06 15:48:17 字数 939 浏览 1 评论 0原文

您好,我正在尝试使用 MenuItem.Icon 通过样式设置器设置:

<Style x:Key="MenuItem_Delete" TargetType="MenuItem"
        BasedOn="{StaticResource {x:Type MenuItem}}">
    <Setter Property="Header" Value="_Delete"/>
    <Setter Property="MenuItem.Icon">
        <Setter.Value>
            <Image Source="Resources/Delete.png"/>
        </Setter.Value>
    </Setter>
</Style>

我在运行时收到以下异常: 无法将“System.Windows.Controls.Image”类型的内容添加到“System.Object”类型的对象。标记文件“WpfApplication1;component/application.xaml”第 164 行第 26 行中的对象“System.Windows.Controls.Image”出错。

另一方面,这是上面链接中的示例:

<MenuItem Header="New">
  <MenuItem.Icon>
    <Image Source="data/cat.png"/>
  </MenuItem.Icon>
</MenuItem>

谢谢。

Hi I am trying to have a MenuItem.Icon set thru a style setter:

<Style x:Key="MenuItem_Delete" TargetType="MenuItem"
        BasedOn="{StaticResource {x:Type MenuItem}}">
    <Setter Property="Header" Value="_Delete"/>
    <Setter Property="MenuItem.Icon">
        <Setter.Value>
            <Image Source="Resources/Delete.png"/>
        </Setter.Value>
    </Setter>
</Style>

I get the following exception at runtime:
Cannot add content of type 'System.Windows.Controls.Image' to an object of type 'System.Object'. Error at object 'System.Windows.Controls.Image' in markup file 'WpfApplication1;component/application.xaml' Line 164 Position 26.

In the other hand, this is the example in the above link:

<MenuItem Header="New">
  <MenuItem.Icon>
    <Image Source="data/cat.png"/>
  </MenuItem.Icon>
</MenuItem>

Thanks.

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

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

发布评论

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

评论(3

傲影 2024-08-13 15:48:17

我遇到了同样的问题。
我在另一个线程 http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/81a106dd-4d06-4506-820a-30fe96a39112
根据他们的解决方案,你可以尝试这个。但绑定仅对 MenuItem 集合中的最后一个元素执行。太糟糕了!

<Style x:Key="MenuItem_Delete" TargetType="MenuItem"
    BasedOn="{StaticResource {x:Type MenuItem}}">
    <Style.Resources>
        <Image x:key="DeleteIcon" Source="Resources/Delete.png"/>
    </Style.Resources>
    <Setter Property="Header" Value="_Delete"/>
    <Setter Property="MenuItem.Icon" Value="{DynamicResource DeleteIcon}" />
</Style>

有更新吗?谢谢!

I have run into the same issue.
I found the same error on aonther thread http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/81a106dd-4d06-4506-820a-30fe96a39112.
According to their solution, you may try this one. But binding executes only for last element in MenuItem collection. It's so bad!

<Style x:Key="MenuItem_Delete" TargetType="MenuItem"
    BasedOn="{StaticResource {x:Type MenuItem}}">
    <Style.Resources>
        <Image x:key="DeleteIcon" Source="Resources/Delete.png"/>
    </Style.Resources>
    <Setter Property="Header" Value="_Delete"/>
    <Setter Property="MenuItem.Icon" Value="{DynamicResource DeleteIcon}" />
</Style>

Is there any update? Thanks!

平定天下 2024-08-13 15:48:17

我拼命在网上寻找答案,我认为这是一个 WPF 错误。

我已向 Microsoft Connect 报告了该问题,请投票并验证或如果您有想法,请与 Microsoft 分享您的想法。

更新
这篇文章对我帮助很大。

I was desperatly searching the web for an answer and I think this is a WPF bug.

I reported it @ Microsoft Connect, please vote and validate or share your ideas with Microsoft if you have some.

Update
This post helped me a lot.

阪姬 2024-08-13 15:48:17

下一个代码将解决这个问题。

<Style x:Key="StyleNewContext" TargetType="MenuItem">
    <Style.Resources>
        <Image x:Key="ImageNewContext" Source="{StaticResource ImageSourceNewContext}" />
        <Image x:Key="ImageNewContextDisabled" Source="{StaticResource ImageSourceNewContextDisabled}" />
    </Style.Resources>
    <Setter Property="Icon" Value="{DynamicResource ImageNewContext}" />
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Icon" Value="{DynamicResource ImageNewContextDisabled}" />
        </Trigger>
    </Style.Triggers>
</Style>

问候,
彼得

The next code will solve this issue.

<Style x:Key="StyleNewContext" TargetType="MenuItem">
    <Style.Resources>
        <Image x:Key="ImageNewContext" Source="{StaticResource ImageSourceNewContext}" />
        <Image x:Key="ImageNewContextDisabled" Source="{StaticResource ImageSourceNewContextDisabled}" />
    </Style.Resources>
    <Setter Property="Icon" Value="{DynamicResource ImageNewContext}" />
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Icon" Value="{DynamicResource ImageNewContextDisabled}" />
        </Trigger>
    </Style.Triggers>
</Style>

Regards,
Peter

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文