0

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.

0

1 Answer 1

1

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}}" />
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! That's exactly what I needed. PS: in my case, I just had to change the UIElement type of properties to DrawingImage in order to fit my requirements.
My only reason for using UIElement is that I have an alternative implementation using Path objects rather than Image controls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.