【发布时间】:2012-12-18 15:27:10
【问题描述】:
我使用以下问题中描述的方法来填充组合框。
ComboBox ItemTemplate does not support values of type 'Image'
ComboBox 填充有包含图像和一些数据的包装类的实例。但是,正如该问题的发布者所说,图像不会显示在组合框中,但是选择其中一个空白条目确实可以正确绑定数据。
数据绑定在模型视图中,因为它与外部数据源交互,但是图像是特定于 UI 的,因此绑定在用户控件的代码中。
如何让图片显示?
代码:
public ObservableCollection<Wrapper> Images { get; set; }
public Class Wrapper
{
public Wrapper(int data, Image img)
{
Data = data;
Img = img;
}
public int Data { get; set; }
public Image Img { get; set; }
}
int selectedData;
public int SelectedData
{
get { return selectedData; }
set
{
selectedData = value;
(this.Resources["model"] as MyModel).External = Images[selectedData].Data;
SendPropertyChanged("SelectedData");
}
}
xaml:
<ComboBox ItemsSource="{Binding Images}" SelectedIndex="{Binding SelectedData}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Image Source="{Binding Img}" Height="83" Width="71"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
【问题讨论】:
标签: wpf image binding combobox