Setting Image Source dynamically

May 11, 2011 at 1:17 PM

Hi folks,

 

first of all: Thanks for sharing your work! I currently run in a problem, which I hope you can help me to solve.

I have a menuitem, that displays the current state of an object. The state is an enumeration and I use a ValueConverter (State2String) to use it for the header:

<SL4PopupMenu:PopupMenuItem x:Name="PopupMenuItemManageState" Header="{Binding State, Converter={StaticResource State2String}, FallbackValue=State}" ImageSourceForRightMargin="/ServiceModeller;component/Assets/arrows/menu-arrow.png">

Whenever the state changes, the menuitem displays the string, that is returned by the value converter. Works as expected.

 

Then I created a Submenu and in this submenu, I created a menuitem for each state. Now the user can change the state by opening the submenu and clicking on the state she wishes for. The special thing I want for the items in this submenu is, that the item, that represents the current active state has a little symbol. I used the ImageSource to display it and again used Binding to set the ImageSource. This works well when the SL-application and the menu are loaded the first time but it doesn't recognize the change of the state. I set a breakpoint in the ValueConverter-method and it only is hit when the application and menu is loaded. It seems, that ImageSource doesn't recognize the PropertyChanged event and so doesn't fetch the new value.

How could I accomplish the described behaviour? (dynamically adapt image source depending on the state property)

 

And here is some code, the menu:

<SL4PopupMenu:PopupMenuItem x:Name="PopupMenuItemManageState" Header="{Binding State, Converter={StaticResource State2String}, FallbackValue=State}" ImageSourceForRightMargin="/ServiceModeller;component/Assets/arrows/menu-arrow.png">
   <SL4PopupMenu:PopupMenu x:Name="PopupMenuState">
     <ListBox BorderBrush="{x:Null}" HorizontalAlignment="Left" VerticalAlignment="Top">
       <SL4PopupMenu:PopupMenuItem x:Name="PopupMenuItemStateInProgress" Header="In Progress" ImageSource="{Binding State, ConverterParameter=InProgress, Converter={StaticResource State2ImageSource}}" Click="PopupMenuItemStandard_Click" />
       <SL4PopupMenu:PopupMenuItem x:Name="PopupMenuItemStateFinal" Header="Final" ImageSource="{Binding State, ConverterParameter=Final, Converter={StaticResource State2ImageSource}}" Click="PopupMenuItemStandard_Click"/>
       <SL4PopupMenu:PopupMenuItem x:Name="PopupMenuItemStateConfirmed" Header="Confirmed" ImageSource="{Binding State, ConverterParameter=Confirmed, Converter={StaticResource State2ImageSource}}" Click="PopupMenuItemStandard_Click"/>
     </ListBox>
   </SL4PopupMenu:PopupMenu>
</SL4PopupMenu:PopupMenuItem>

 

and the corresponding ValueConverter:

 

public class ConvertState2ImageSource : IValueConverter
{
   public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
   {
     ServiceModeller.WCFDataProvider.WorkProgressionState state = (ServiceModeller.WCFDataProvider.WorkProgressionState)value;
     string parm = (string)parameter;

     switch (parm)
     {
       case "InProgress":
         if (state == WCFDataProvider.WorkProgressionState.InProgress)
           return "/ServiceModeller;component/Assets/arrows/menu-arrow.png";
         else
           return String.Empty;
       case "Final":
         if (state == WCFDataProvider.WorkProgressionState.Final)
           return "/ServiceModeller;component/Assets/arrows/menu-arrow.png";
         else
           return String.Empty;
       case "Confirmed":
         if (state == WCFDataProvider.WorkProgressionState.Confirmed)
           return "/ServiceModeller;component/Assets/arrows/menu-arrow.png";
         else
           return String.Empty;
       default:
         throw new Exception("Parameter '" + parm + "' unknown.");
     }
   }

   public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
   {
     return null;
   }
}

Thanks in advance,

Frank

Coordinator
May 13, 2011 at 12:37 PM

Hi Aaginor,

I've just released version 2.6 of the menu. Can you please upgrade and let me know if this tackled it?

By the way note that the property ImageSourceForRightMargin is now ImageRightSource. So you will have to do some text replacement in your code as well but its definitely worth it.

Regards

Ziad

May 18, 2011 at 4:24 PM

Hi Ziad,

 

thanks for the reply! I  updated the menu but unfortunately it didn't fix the problem. The ImageSource property is set only during the initial binding (so I am sure, that the binding works) and doesn't update, when the bound value changes. So it looks like the ImageSource property doesn't react on the PropertyChanged event. If there is no quick fix to this, I will change the structure a bit, so that this feature isn't needed anymore, so no worries!

 

Ciao,

Frank