Я думаю, что лучший способ сделать это - использовать такой конвертер:
public class EnumToResource : IValueConverter
{
public List<object> EnumMapping { get; set; }
public EnumToResource()
{
EnumMapping = new List<object>();
}
public virtual object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int adjustment = 0;
if (parameter != null && !Int32.TryParse(parameter.ToString(), out adjustment))
{
adjustment = 0;
}
if (value == null) return this.EnumMapping.ElementAtOrDefault(0);
else if (value is bool)
return this.EnumMapping.ElementAtOrDefault(System.Convert.ToByte(value) + adjustment);
else if (value is byte)
return this.EnumMapping.ElementAtOrDefault(System.Convert.ToByte(value) + adjustment);
else if (value is short)
return this.EnumMapping.ElementAtOrDefault(System.Convert.ToInt16(value) + adjustment);
else if (value is int)
return this.EnumMapping.ElementAtOrDefault(System.Convert.ToInt32(value) + adjustment);
else if (value is long)
return this.EnumMapping.ElementAtOrDefault(System.Convert.ToInt32(value) + adjustment);
else if (value is Enum)
return this.EnumMapping.ElementAtOrDefault(System.Convert.ToInt32(value) + adjustment);
return this.EnumMapping.ElementAtOrDefault(0);
}
public virtual object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Затем объявите перечисление с именем NodeType:
enum NodeType
{
Folder,
File,
}
В вашей модели представления вы объявляете свойство INotifyPropertyChanged с именем NodeType типа перечисления.
Затем в вашем XAML вы объявляете ресурс конвертера следующим образом:
<Converters:EnumToResource x:Key="IconConverter">
<Converters:EnumToResource.EnumMapping>
<BitmapImage UriSource="\Resources\Folder.png"/>
<BitmapImage UriSource="\Resources\File.png"/>
</Converters:EnumToResource.EnumMapping>
</Converters:EnumToResource>
Наконец, вы связываете свою собственность так:
<Image Source="{Binding Path=NodeType, Converter={StaticResource ResourceKey=IconConverter}}"/>
Таким образом, вам не нужно иметь дело с объявлениями и загрузкой BitmapImage в вашей модели представления, и вы все равно можете сделать его полностью привязываемым.