Reputation: 10624
I have a button with an image inside it. This button appears on a datagrid many times for displaying the status of the row. When the user clicks the button, it changes the state of the underlying object on the row to enabled or disabled. Here is what the button looks like:
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button CommandParameter="{Binding}" HorizontalAlignment="Center">
<Image Source="{Binding Converter={StaticResource EnableDisableConverter}}" Height="25" Width="25" />
</Button>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
The converter correctly returns the proper image based on the status. The problem is that I've switched to an MVVM model and my code for changing the image won't work anymore. My previous looked like this:
Image img = (Image)btn.Content;
if (c.Status == Administration.Web.ObjectStatus.Enabled) {
img.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("/Images/enable-icon.png", UriKind.Relative));
} else {
img.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("/Images/disable-icon.png", UriKind.Relative));
}
During the command that changes the status, I've tried raising a change to the property that contains the object, but it doesn't reflect it on the UI. If I do a hard refresh of the screen, the status correctly changes. Is there a way to have rebind the image in the current situation?
Upvotes: 1
Views: 936
Reputation: 4878
Bind the source of the image to some bool Enabled property and your EnableDisableConverter can than react to that value, after each change.
XAML:
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button CommandParameter="{Binding}" HorizontalAlignment="Center">
<Image Source="{Binding IsEnabled, Converter={StaticResource EnableDisableConverter}}" Height="25" Width="25" />
</Button>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
ViewModel:
...
public bool IsEnabled
{
get
{
return _isEnabled;
}
set
{
_isEnabled=value;
NotifyPropertyChanged("IsEnabled");
}
}
...
Converter:
public object Convert(object value, ...)
{
if((bool)value)
return uri of image1;
else
return uri of image2;
}
But I don't know what are the objects in the grid and what is the ViewModel. There may be a problem. The point is to have that IsEnabled property corectly binded to those objects in the grid.
Upvotes: 2