【发布时间】:2014-05-15 10:53:51
【问题描述】:
我有一个组合框,其中有一些项目,例如:
项目1
项目2
项目3
对应每个item都有图片,比如item1有图片img1.jpg,item2有图片img2.jpg,item3有图片img3.jpg。当我们从combox中选择item时,它会在label中显示对应的图片。
【问题讨论】:
我有一个组合框,其中有一些项目,例如:
项目1
项目2
项目3
对应每个item都有图片,比如item1有图片img1.jpg,item2有图片img2.jpg,item3有图片img3.jpg。当我们从combox中选择item时,它会在label中显示对应的图片。
【问题讨论】:
我的问题得到了答案,这里是:
<xmlns:local="clr-namespace:ImageMVVM_Learning"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:EnumToImageConverter x:Key="conv"/>
</Window.Resources>
<Grid>
<StackPanel>
<ComboBox x:Name="combo" ItemsSource="{Binding MyProperty}"/>
<Image Source="{Binding ElementName=combo,Path=SelectedValue,Converter={StaticResource conv}}"/>
</StackPanel>
</Grid>
</Window>
在您的视图模型类中执行此操作:
public enum MyEnum
{
A,
B,
C
}
public class EnumToImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null)
{
switch ((MyEnum)value)
{
case MyEnum.A:
return new BitmapImage(new Uri(@"Images\A.png", UriKind.Relative));
case MyEnum.B:
return new BitmapImage(new Uri(@"Images\B.png", UriKind.Relative));
}
}
return new BitmapImage(new Uri(@"Images\A.png", UriKind.Relative));
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
【讨论】: