24

I am trying to use an enum to display a corresponding image. For this I have a value converter that converts an enum to the correct resource name. My resources are defined as follows:

<UserControl.Resources>
    <BitmapImage x:Key="AlarmCat1" UriSource="/Lib.Infrastructure;component/Resources/msg_cat1.bmp" />
    <BitmapImage x:Key="AlarmCat2" UriSource="/Lib.Infrastructure;component/Resources/msg_cat2.bmp" />
    <BitmapImage x:Key="AlarmCat3" UriSource="/Lib.Infrastructure;component/Resources/msg_cat3.bmp" />
    <converters:JamCategoryToImageConverter x:Key="AlarmCategoryConverter" />
</UserControl.Resources>

This works:

<Image Source="{StaticResource AlarmCat1}" />

But this doesn't, the converter is called and the correct value is passed back. What is the correct syntax?

<Image Source="{StaticResource { Binding CurrentAlarmItem.AlarmCategory, Converter={StaticResource AlarmCategoryConverter}}}" />

For completeness, this is the convert function:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    switch ((AlarmCategory)value)
    {
        case AlarmCategory.Category1:
            return "AlarmCat1";
        case AlarmCategory.Category2:
            return "AlarmCat2";
        case AlarmCategory.Category3:
            return "AlarmCat3";
        default:
            return null;
    }
}
uncletall
  • 6,609
  • 1
  • 27
  • 52

2 Answers2

31

I would return the resource in the converter:

<Image Source="{Binding CurrentAlarmItem.AlarmCategory, Converter={StaticResource AlarmCategoryConverter}}" />

In your converter do something like this:

return Application.Current.FindResource("AlarmCat1") as BitmapImage;

Set your resources for the complete application with the use of resourcedictionary (app.xaml)

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Dictionary1.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

In your Dictionary (Dictionary1.xaml)

<BitmapImage x:Key="AlarmCat1" UriSource="bh.jpg" />

Because your resources are now defined on applicationlevel, the code will now find your resource and give it back.

  • Just deleted my converter... but I am going to try your solution as well. Look more intuitive – uncletall Nov 29 '13 at 07:14
  • It might not work than. I am sure you can get it to work with a bit of other code, but if the above answer works, I should use that ;-) – Bram Van Strydonck Nov 29 '13 at 07:54
  • Well, you solution is one line of xaml, the above is 17 lines.... I can move the resource, but should I use the name specified in `User.Control` `BitmapImage` as you can see in my original question? Or the filename? – uncletall Nov 29 '13 at 07:58
  • The easiest way to make it work with my answer is to change the location of your resources. (I updated my answer) – Bram Van Strydonck Nov 29 '13 at 08:13
  • No need to make the resource local. I can just add the resource definition from my usercontrol to the resource dictionary as you described and it works. Beauty is that it also works at design time! – uncletall Nov 29 '13 at 08:41
  • Oh, the Findresource["AlarmCat1"] should be FindResource("AlarmCat1"). Maybe you can update this for future references. Thanks a lot for you help! – uncletall Nov 29 '13 at 08:42
8

You cannot bind the StaticResource Key as it is not the DependancyProperty. Either you will have to Bind Source directly to the enum using converter and update the converter code to return the Bitmap itself.

Second option will be to used Triggers to set the Source property depending on the enum value.

<Image >
   <Image.Style>
      <Style TargetType="{x:Type Image}">
         <Style.Triggers>
            <DataTrigger Binding="{Binding CurrentAlarmItem.AlarmCategory}" 
                         Value="Category1">
               <Setter Property="Source" Value="{StaticResource AlarmCat1}" />
            </DataTrigger>
         </Style.Triggers>
      </Style>
   </Image.Style>
</Image>
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
Nitin
  • 18,344
  • 2
  • 36
  • 53