Wpf 如何正确地将图标或图像添加到Mahapp DropDownButton中的每个菜单项

Wpf 如何正确地将图标或图像添加到Mahapp DropDownButton中的每个菜单项,wpf,mahapps.metro,Wpf,Mahapps.metro,我正在使用 以下是我所拥有的: 使用 具有: public class SomeDefinition { public int Id { get; set; } public string Title{ get; set; } public ICommand Command { get; set; } public string IconPath { get; set; } } 这只显示菜单上最后一个值的图像,我无

我正在使用

以下是我所拥有的:

使用

具有:

public class SomeDefinition
    {
        public int Id { get; set; }
        public string Title{ get; set; }
        public ICommand Command { get; set; }
        public string IconPath { get; set; }
    }
这只显示菜单上最后一个值的图像,我无法找出我缺少了什么。为什么它只显示最后一张记录的图像

我测试了所有图像,它们都写得正确,它们也确实存在,所以问题是找不到图像文件

更新 因此,我尝试使用@mm8解决方案,并将定义类更改为:

public class SomeDefinition
    {
        public int Id { get; set; }
        public string Title{ get; set; }
        public ICommand Command { get; set; }
        public Image IconImage { get; set; }

    }
具有xaml:

<mah:DropDownButton 
    Content="my button" 
    Icon="{DynamicResource appbar_projector_screen}"
    ItemsSource="{Binding Path=MySource}">
    <mah:DropDownButton.ItemContainerStyle>
        <Style TargetType="MenuItem">
            <Setter Property="Header" Value="{Binding Path=Title}"/>
            <Setter Property="Command" Value="{Binding Path=Command}"/>
            <Setter Property="CommandParameter" Value="{Binding Path=Id }"/>
            <Setter Property="Icon" Value="{Binding IconImage}" />          
        </Style>
    </mah:DropDownButton.ItemContainerStyle>
</mah:DropDownButton>

和代码隐藏:

var Image = new Bitmap(@"D:\\1.png");



MySource = new List<SomeDefinition>
{
    new SomeDefinition{Id=1, Title= $@"1", Command = Command1, IconImage = Image},
    new SomeDefinition{Id=2, Title= $@"2", Command = Command2, IconImage = Image},
    new SomeDefinition{Id=3, Title= $@"3", Command = Command3, IconImage = Image},
    new SomeDefinition{Id=4, Title= $@"4", Command = Command4, IconImage = Image},
    new SomeDefinition{Id=5, Title= $@"5", Command = Command5, IconImage = Image}
};
var Image=新位图(@“D:\\1.png”);
MySource=新列表
{
新的SomeDefinition{Id=1,Title=$@“1”,Command=Command1,IconImage=Image},
新的SomeDefinition{Id=2,Title=$@“2”,Command=Command2,IconImage=Image},
新的SomeDefinition{Id=3,Title=$@“3”,Command=Command3,IconImage=Image},
新的SomeDefinition{Id=4,Title=$@“4”,Command=Command4,IconImage=Image},
新的SomeDefinition{Id=5,Title=$@“5”,Command=Command5,IconImage=Image}
};
结果是:

如何将对象转换为图像?

“这仅显示菜单上最后一个值的图像”-Setter仅创建一个图像实例,只能在一个位置显示

作为一种解决方法,您可以将图像声明为非共享资源,并通过Setter中的StaticResource使用它:

<Image x:Key="StdIcon" x:Shared="False" Width="25" Height="25" Source="{Binding IconPath}" />

<Setter Property="Icon" Value="{StaticResource StdIcon}"/>


您是否尝试将
图标
属性绑定到您的
图标路径
源属性?:
@mm8如果我这样做,我确实会为所有菜单项显示urlPath,但我想要的是图像而不是url,这是因为我使用图像源…我应该在哪里添加图像声明?在参考资料中,在样式之前<代码>
public class SomeDefinition
    {
        public int Id { get; set; }
        public string Title{ get; set; }
        public ICommand Command { get; set; }
        public Image IconImage { get; set; }

    }
<mah:DropDownButton 
    Content="my button" 
    Icon="{DynamicResource appbar_projector_screen}"
    ItemsSource="{Binding Path=MySource}">
    <mah:DropDownButton.ItemContainerStyle>
        <Style TargetType="MenuItem">
            <Setter Property="Header" Value="{Binding Path=Title}"/>
            <Setter Property="Command" Value="{Binding Path=Command}"/>
            <Setter Property="CommandParameter" Value="{Binding Path=Id }"/>
            <Setter Property="Icon" Value="{Binding IconImage}" />          
        </Style>
    </mah:DropDownButton.ItemContainerStyle>
</mah:DropDownButton>
var Image = new Bitmap(@"D:\\1.png");



MySource = new List<SomeDefinition>
{
    new SomeDefinition{Id=1, Title= $@"1", Command = Command1, IconImage = Image},
    new SomeDefinition{Id=2, Title= $@"2", Command = Command2, IconImage = Image},
    new SomeDefinition{Id=3, Title= $@"3", Command = Command3, IconImage = Image},
    new SomeDefinition{Id=4, Title= $@"4", Command = Command4, IconImage = Image},
    new SomeDefinition{Id=5, Title= $@"5", Command = Command5, IconImage = Image}
};
<Image x:Key="StdIcon" x:Shared="False" Width="25" Height="25" Source="{Binding IconPath}" />

<Setter Property="Icon" Value="{StaticResource StdIcon}"/>