鉴于您的名字,我怀疑您希望的行为是根据状态属性显示不同的图标,在这种情况下,您确实希望尽可能多地保留代码。
我会建议一个值转换器,您可以在其中输入状态并取回图像源或 URI,然后只需将图像图像源绑定到状态属性
这是一个完整的例子
注意:示例使用 c#6 语法
WPF:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<BitmapImage x:Key="img1" UriSource="someimg1.png" />
<BitmapImage x:Key="img2" UriSource="someimg2.png" />
<BitmapImage x:Key="img3" UriSource="someimg3.png" />
<BitmapImage x:Key="img4" UriSource="someimg4.png" />
<BitmapImage x:Key="img5" UriSource="someimg5.png" />
<local:StateImageConverter
x:Key="StateImageConverter"
State1Image="{StaticResource img1}"
State2Image="{StaticResource img2}"
State3Image="{StaticResource img3}"
State4Image="{StaticResource img4}"
State5Image="{StaticResource img5}"/>
</Window.Resources>
<Window.DataContext>
<local:VeiwModel x:Name="viewModel" />
</Window.DataContext>
<StackPanel>
<ComboBox ItemsSource="{Binding AllStates}" SelectedValue="{Binding State}"/>
<Image Source="{Binding State, Converter={StaticResource StateImageConverter}}" />
</StackPanel>
</Window>
代码:
public enum States
{
State1,
State2,
State3,
State4,
State5,
}
public class StateImageConverter : IValueConverter
{
public ImageSource State1Image { get; set; }
public ImageSource State2Image { get; set; }
public ImageSource State3Image { get; set; }
public ImageSource State4Image { get; set; }
public ImageSource State5Image { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var state = value as States?;
if(state.HasValue)
{
switch (state.Value)
{
case States.State1:
return State1Image;
case States.State2:
return State2Image;
case States.State3:
return State3Image;
case States.State4:
return State4Image;
case States.State5:
return State5Image;
default:
throw new InvalidCastException();
}
}
else
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
//one way only
throw new NotImplementedException();
}
}
public class VeiwModel : System.ComponentModel.INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private States state;
public States State
{
get { return state; }
set
{
state = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("State"));
}
}
public IEnumerable<States> AllStates=> Enum.GetValues(typeof(States)).OfType< States>();
}
或者,如果您不想使用命名属性,那么使用列表的变体将如下所示:
public class StateImageConverter : IValueConverter
{
public ObservableCollection<ImageSource> Images { get; set; } = new ObservableCollection<ImageSource>();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var state = value as States?;
if(state.HasValue)
{
return Images[(int)state.Value];
}
else
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
//one way only
throw new NotImplementedException();
}
}
定义为
<local:StateImageConverter
x:Key="StateImageConverter">
<local:StateImageConverter.Images>
<BitmapImage UriSource="someimg1.png" />
<BitmapImage UriSource="someimg2.png" />
<BitmapImage UriSource="someimg3.png" />
<BitmapImage UriSource="someimg4.png" />
<BitmapImage UriSource="someimg5.png" />
</local:StateImageConverter.Images>
</local:StateImageConverter>