组合框不显示图像

ComboBox not showing images

我正在尝试用 xaml 中的图像创建一个组合框,但图像就是不显示。我不确定我做错了什么但我已经按照几个不同的教程尝试了很多不同的东西(例如改变数据传递到组合框的方式,改变图像文件格式等......)没有成功...

这是我的代码:

在XAML中:

<ComboBox x:Name="cbFloor" HorizontalAlignment="Left" VerticalAlignment="Top" Height="30" Width="120" >
                                <ComboBox.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <Image Source="{Binding Image}" Width="30" Height="30"/>
                                            <TextBlock Text="{Binding Label}" VerticalAlignment="Center" Padding="10 0 0 0"/>
                                        </StackPanel>
                                    </DataTemplate>
                                </ComboBox.ItemTemplate>
                            </ComboBox>

在 C# 中:

public partial class Gui : Window
{
     public Gui()
     {
          InitializeComponent();

          string imageDirectory = //string directory to a folder on my PC
          // I tried using different file formats to see if that was the problem. None of them works.
          cbFloor.Items.Add(new PictureItem(new BitmapImage(new Uri(imageDirectory + "wood.jpg", UriKind.Relative)), 
                                            "Hardwood"));
          cbFloor.Items.Add(new PictureItem(new BitmapImage(new Uri(imageDirectory + "linoleum.png", UriKind.Relative)), 
                                            "Linoleum"));
          cbFloor.Items.Add(new PictureItem(new BitmapImage(new Uri(imageDirectory + "icon16.bmp", UriKind.Relative)), 
                                            "Stone Tiles"));
     }
}


public class PictureItem
{
    public BitmapImage Image { get; set; }
    public string Label { get; set; }

    public PictureItem(BitmapImage BitImg, string label)
    {
        Image = BitImg;
        Label = label;
    }
}

这就是我得到的:

我没有想法...任何帮助将不胜感激。谢谢!

UriKind.Relative 会出现问题。删除它或使用 UriKind.Absolute.

cbFloor.Items.Add(new PictureItem(new BitmapImage(new Uri(imageDirectory + "wood.jpg")), "Hardwood"));
cbFloor.Items.Add(new PictureItem(new BitmapImage(new Uri(imageDirectory + "linoleum.png")), "Linoleum"));
cbFloor.Items.Add(new PictureItem(new BitmapImage(new Uri(imageDirectory + "icon16.bmp")), "Stone Tiles"));

或者

cbFloor.Items.Add(new PictureItem(new BitmapImage(new Uri(imageDirectory + "OckovaciPrukaz.jpg", UriKind.Absolute)), "Hardwood"));
cbFloor.Items.Add(new PictureItem(new BitmapImage(new Uri(imageDirectory + "linoleum.png", UriKind.Absolute)), "Linoleum"));
cbFloor.Items.Add(new PictureItem(new BitmapImage(new Uri(imageDirectory + "icon16.bmp", UriKind.Absolute)), "Stone Tiles"));