0

我正在使用Mahapp 的 DropDownButton

这是我所拥有的:

使用

 xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"

<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">
                <Setter.Value>
                    <Image Width="25" Height="25" Source="{Binding IconPath}" />
                </Setter.Value>
            </Setter>
        </Style>
    </mah:DropDownButton.ItemContainerStyle>
</mah:DropDownButton>

首先我尝试使用:

<Setter Property="Header">
    <Setter.Value>
        <StackPanel>
            <Image Width="20" Height="20" Source="{Binding IconPath}" />
            <TextBlock Text="{Binding Path=Title}" />
        </StackPanel>
    </Setter.Value>
</Setter>
                        

但是该代码没有显示任何图像。所以我尝试了:

<Setter Property="Icon">
    <Setter.Value>
        <Image Width="25" Height="25" Source="{Binding IconPath}" />
    </Setter.Value>
</Setter>

初始化对象MySource

public List<SomeDefinition> MySource{ get; private set; }

MySource= new List<SomeDefinition>
{
    new SomeDefinition{Id=1, Title= $@"1", Command = new RelayCommand<object>(SomeMethod1), IconPath = "D:\\ico1.png"},
    new SomeDefinition{Id=2, Title= $@"2", Command = new RelayCommand<object>(SomeMethod2), IconPath = "D:\\ico2.png"},
    new SomeDefinition{Id=3, Title= $@"3", Command = new RelayCommand<object>(SomeMethod3), IconPath = "D:\\ico3.png"},
    new SomeDefinition{Id=4, Title= $@"4", Command = new RelayCommand<object>(SomeMethod4), IconPath = "D:\\ico4.png"},
    new SomeDefinition{Id=5, Title= $@"5", Command = new RelayCommand<object>(SomeMethod5), IconPath = "D:\\ico5.png"}
};

有:

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; }

    }

有xml:

<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}
};

结果是: 在此处输入图像描述

如何将对象转换为图像?

4

1 回答 1

2

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

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

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

<Setter Property="Icon" Value="{StaticResource StdIcon}"/>
于 2019-12-10T08:33:16.300 回答