Reputation: 624
I want to change the image displayed in a grid depending on a certain condition (e.g. an enum value). The problem is that the image sources are DrawingImage (provided from xaml file).
<Grid>
<Image Source="{StaticResource BodyDrawingImage}"></Image>
</Grid>
I want that BodyDrawingImage (which is a key for DrawingImage) to be selected at run-time (by a binding i guess) from a list of DrawingImage keys but I can't figure out how to do this.
Upvotes: 0
Views: 730
Reputation: 4546
The way I've handled this is using a value converter class, with a property corresponding to each element of the Enum type.
public class perDialogIconConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is perDialogIcon))
{
return null;
}
switch ((perDialogIcon) value)
{
case perDialogIcon.Asterisk:
return AsteriskIcon;
case perDialogIcon.Error:
return ErrorIcon;
case perDialogIcon.Exclamation:
return ExclamationIcon;
case perDialogIcon.Hand:
return HandIcon;
case perDialogIcon.Information:
return InformationIcon;
case perDialogIcon.Question:
return QuestionIcon;
case perDialogIcon.Stop:
return StopIcon;
case perDialogIcon.Warning:
return WarningIcon;
default:
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public UIElement AsteriskIcon { get; set; }
public UIElement ErrorIcon { get; set; }
public UIElement ExclamationIcon { get; set; }
public UIElement HandIcon { get; set; }
public UIElement InformationIcon { get; set; }
public UIElement QuestionIcon { get; set; }
public UIElement StopIcon { get; set; }
public UIElement WarningIcon { get; set; }
}
This is instantuated as a resource within the window, along with each image required
<Window.Resources>
<Image x:Key="AsteriskIcon"
Source="Images/Asterisk.png"
Stretch="None" />
<Image x:Key="ErrorIcon"
Source="Images/Error.png"
Stretch="None" />
<Image x:Key="ExclamationIcon"
Source="Images/Exclamation.png"
Stretch="None" />
<Image x:Key="HandIcon"
Source="Images/Hand.png"
Stretch="None" />
<Image x:Key="InformationIcon"
Source="Images/Information.png"
Stretch="None" />
<Image x:Key="QuestionIcon"
Source="Images/Question.png"
Stretch="None" />
<Image x:Key="StopIcon"
Source="Images/Stop.png"
Stretch="None" />
<Image x:Key="WarningIcon"
Source="Images/Warning.png"
Stretch="None" />
<dlg:perDialogIconConverter x:Key="DialogIconConverter"
AsteriskIcon="{StaticResource AsteriskIcon}"
ErrorIcon="{StaticResource ErrorIcon}"
ExclamationIcon="{StaticResource ExclamationIcon}"
HandIcon="{StaticResource HandIcon}"
InformationIcon="{StaticResource InformationIcon}"
QuestionIcon="{StaticResource QuestionIcon}"
StopIcon="{StaticResource StopIcon}"
WarningIcon="{StaticResource WarningIcon}" />
</Window.Resources>
and then used as required as part of the binding statement - DialogIcon
is an enum property on the ViewModel
<ContentPresenter Grid.Row="0"
Grid.Column="0"
VerticalAlignment="Center"
Content="{Binding DialogIcon, Converter={StaticResource DialogIconConverter}}" />
Upvotes: 1