我正在嘗試用圖像填充組合框。它被定義爲: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];
圖像被嵌入的資源。在這裏我有兩個問題:
- 圖像不顯示;
沒有選擇該項目時,SelectedLangItem是:
公共LanguageItem SelectedLangItem { 得到{_SelectedLangItem; } set { if(_SelectedLangItem == value) return;
_SelectedLangItem = value; this.RaisePropertyChanged("SelectedLangItem"); } }
謝謝@ vitaliy-zadorozhnyy,對於屬性綁定,你是對的!這是一個複製粘貼錯誤! – Barzo
使用以下路徑作爲路徑:「/MyNamespace;component/Images/FLAG1.gif」 – Barzo