2013-08-28 91 views
4

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

<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. 没有选择该项目时,SelectedLangItem是:

    公共LanguageItem SelectedLangItem { 得到{_SelectedLangItem; } set { if(_SelectedLangItem == value) return;

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

回答

3

使用

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

,因为它必须执行有关的ImageSource

而不是选择:属性名称是 “SelectedLangItem”,而在XAML SelectedLangComboItem如果你没有输错。

CODE:

this.RaisePropertyChanged("SelectedLangItem"); 

XAML:

<ComboBox SelectedItem="{Binding SelectedLangComboItem}" 
+0

谢谢@ vitaliy-zadorozhnyy,对于属性绑定,你是对的!这是一个复制粘贴错误! – Barzo

+0

使用以下路径作为路径:“/MyNamespace;component/Images/FLAG1.gif” – Barzo

3

你的问题是,你正在尝试的Image绑定到Image.Source属性,这是ImageSource类型。

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

​​

然后你就可以正确地将此字符串(框架将转换为ImageSource对象)绑定到您的DataTemplate中的Image.Source属性。

+0

我只想指出'Image'属性返回一个'System.Drawing.Bitmap',它来自WinForms。如果该属性返回一个WPF'ImageSource',这将正常工作。 –

+0

@AbeHeidebrecht,你指的是谁?如果是我(似乎就像我对你的评论一样)...我已经知道了,谢谢......如果是问题作者,那么也许你的评论会更适合,如果它是反而加入他们的问题? – Sheridan

+0

嗨,感谢所有的建议。我有点困惑:-)我将ImageSource设置为LanguageItem.Image的类型,并按照@ vitaliy-zadorozhnyy的建议更改我的代码,但图像仍未显示。 – Barzo

0

试试下面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/