I used the other answers in this topic to puzzle together the solution I needed for my MVVM application. The code is used to flip the image and tooltip of a toolbar button (Hide/Unhide in this case). It might help you if I put it all together here.
First, the declaration of the button in the xaml file:
<Button ToolTip="{Binding ButtonText}">
<Image Height="32" Width="32" Source="{Binding ButtonImage}"/>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:CallMethodAction TargetObject="{Binding}" MethodName="HideAction"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
In my ViewModel class, I declared:
private BitmapImage _buttonImage;
public BitmapImage ButtonImage
{
get { return _buttonImage; }
set
{
_buttonImage = value;
OnPropertyChanged("ButtonImage");
}
}
private string _buttonText;
public string ButtonText
{
get { return _buttonText; }
set
{
_buttonText = value;
OnPropertyChanged("ButtonText");
}
}
Here the code for the event handler that changes the button:
public void HideAction()
{
// Hide the thing you want to hide
...
// Flip the button
if (ButtonText == "Hide")
{
ButtonImage = new BitmapImage(new Uri(@"pack://application:,,,/Resources/Unhide32.png", UriKind.RelativeOrAbsolute));
ButtonText = "Unhide";
}
else
{
ButtonImage = new BitmapImage(new Uri(@"pack://application:,,,/Resources/Hide32.png", UriKind.RelativeOrAbsolute));
ButtonText = "Hide";
}
}
In the constructor of my ViewModel class I initialize the image and tooltip:
ButtonImage = new BitmapImage(new Uri(@"pack://application:,,,/Resources/Hide32.png", UriKind.RelativeOrAbsolute));
ButtonText = "Hide";