我正在尝试用图像填充一个组合。定义如下:
<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;
}
}
现在,在我的视图模型中,我做了:
_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];
图像是嵌入式资源。这里我有两个问题:
图像不显示;
未选择项,所选语言项为:
公共语言项selectedlangitem
{
获取{return\u selectedlangitem;}
设置
{
如果(_selectedlangitem==值)
回报;
_SelectedLangItem = value;
this.RaisePropertyChanged("SelectedLangItem");
}
}
最佳答案
使用
new BitmapImage(new Uri("MyNamespace.Images.FLAG1.gif", UriKind.Relative));
因为它必须实现imagesource
关于未选择:属性名是“selectedlangitem”,而在xaml中,selectedlangcomboitem是“selectedlangitem”。
代码:
this.RaisePropertyChanged("SelectedLangItem");
xaml:
<ComboBox SelectedItem="{Binding SelectedLangComboItem}"