C# 带有图像的 WPF 组合框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18491205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:23:29  来源:igfitidea点击:

WPF ComboBox with image

c#wpfcombobox

提问by Barzo

I'm trying to populate a Combo with images. It is defined as:

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

<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>

Where the items are the LanguageItem classes:

其中项目是 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;
  }
}

Now, in my ViewModel c'tor I do:

现在,在我的 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];

The images are embedded resources. Here I have two problems:

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

  1. The images are not displayed;
  2. The Item is not selected, the SelectedLangItem is:

    public LanguageItem SelectedLangItem { get { return _SelectedLangItem; } set { if (_SelectedLangItem == value) return;

        _SelectedLangItem = value;
        this.RaisePropertyChanged("SelectedLangItem");
      }
    }
    
  1. 图像不显示;
  2. Item 没有被选中,SelectedLangItem 是:

    public LanguageItem SelectedLangItem { get { return _SelectedLangItem; } set { if (_SelectedLangItem == value) return;

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

采纳答案by vitaliy zadorozhnyy

Use

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

as it have to implement ImageSource

因为它必须实现 ImageSource

And regarding not selected: Property name is "SelectedLangItem" while in xaml SelectedLangComboItem if you did not mistype.

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

CODE:

代码:

this.RaisePropertyChanged("SelectedLangItem");

XAML:

XAML:

<ComboBox SelectedItem="{Binding SelectedLangComboItem}"

回答by Sheridan

Your problem is that you are trying to bind an Imageto the Image.Sourceproperty, which is of type ImageSource.

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

The easiest solution is to add your actual image files into a folder and change the Imageproperty in your class to a string that holds the file path to the image in this format:

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

/ApplicationName;component/ImageFolderName/ImageName.png

Then you can correctly bind this string (which the Framework will convert into an ImageSourceobject) to the Image.Sourceproperty in your DataTemplate.

然后你就可以正确绑定此字符串(该框架将转换为ImageSource对象),将Image.Source你的财产DataTemplate

回答by Code Scratcher

Try below xaml code and bind image list into combobox...

尝试以下 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>

Check below... for more understanding of live example...

检查下面...以进一步了解现场示例...

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

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