8

我正在尝试用图像填充组合。它被定义为:

<ComboBox SelectedItem="{Binding SelectedLangComboItem}"
          ItemsSource="{Binding Languages}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Image}" />
                <TextBlock Text="{Binding Label}" />
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

其中项目是 LanguageItem 类:

public class LanguageItem
{
  public System.Drawing.Bitmap Image { get; set; }
  public string Label { get; set; }
  public string Culture { get; set; }

  public LanguageItem(System.Drawing.Bitmap image, string label, string culture)
  {
    Image = image;
    Label = label;
    Culture = culture;
  }
}

现在,在我的 ViewModel c'tor 我做:

  _Languages = new ObservableCollection<LanguageItem>();

  System.Reflection.Assembly app = System.Reflection.Assembly.GetExecutingAssembly();
  System.IO.Stream file;
  file = app.GetManifestResourceStream("MyNamespace.Images.FLAG1.gif");
  _Languages.Add(new LanguageItem(new Bitmap(file), "ITALIAN", "it-IT"));
  file = app.GetManifestResourceStream("MyNamespace.Images.FLAG2.gif");
  _Languages.Add(new LanguageItem(new Bitmap(file), "ENGLISH", "en-EN"));

  this.SelectedLangItem = _Languages[0];

图像是嵌入式资源。这里我有两个问题:

  1. 图像不显示;
  2. Item 没有被选中,SelectedLangItem 是:

    公共 LanguageItem SelectedLangItem { 获取 { 返回 _SelectedLangItem; } 设置 { if (_SelectedLangItem == value) 返回;

        _SelectedLangItem = value;
        this.RaisePropertyChanged("SelectedLangItem");
      }
    }
    
4

3 回答 3

5

您的问题是您试图将 an 绑定Image到类型为 的Image.Source属性ImageSource

最简单的解决方案是将您的实际图像文件添加到一个文件夹中,并将Image您的类中的属性更改为一个字符串,该字符串以这种格式保存图像的文件路径:

/ApplicationName;component/ImageFolderName/ImageName.png

然后,您可以将此字符串(框架将其转换为ImageSource对象)正确绑定Image.SourceDataTemplate.

于 2013-08-28T14:56:36.403 回答
5

利用

new BitmapImage(new Uri("MyNamespace.Images.FLAG1.gif", UriKind.Relative));

因为它必须实现 ImageSource

关于未选择:如果您没有输入错误,则在 xaml SelectedLangComboItem 中的属性名称为“SelectedLangItem”。

代码:

this.RaisePropertyChanged("SelectedLangItem");

XAML:

<ComboBox SelectedItem="{Binding SelectedLangComboItem}"
于 2013-08-28T15:01:00.547 回答
2

尝试下面的 xaml 代码并将图像列表绑定到组合框...

<Window.Resources>
        <DataTemplate x:Key="cmbTemplate">
            <WrapPanel Margin="0 5 0 5" Height="80">
                <Image Width="65" Height="65" Stretch="Fill" Source="{Binding Photo}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,15,0"/>
                <Label Content="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="20"/>
            </WrapPanel>
        </DataTemplate>
    </Window.Resources>

    <StackPanel HorizontalAlignment="Center">
        <Label Content="Dropdown list with Image" HorizontalAlignment="Center" FontSize="30" Margin="20"/>
        <ComboBox x:Name="lstwithimg" HorizontalAlignment="Center" VerticalAlignment="Top" ItemTemplate="{StaticResource cmbTemplate}" Height="80" Width="400"/>
    </StackPanel>

检查下面...更多了解现场示例...

http://www.codescratcher.com/wpf/wpf-combobox-with-image/

于 2015-06-15T17:35:07.950 回答