2011-03-09 26 views
1

我有一个按钮,里面有图像。此按钮多次显示在数据网格上以显示行的状态。当用户单击该按钮时,它将该行上的基础对象的状态更改为启用或禁用。这里是按钮的样子:在使用转换器时重新绑定图像源?

<data:DataGridTemplateColumn.CellTemplate> 
    <DataTemplate> 
     <Button CommandParameter="{Binding}" HorizontalAlignment="Center"> 
      <Image Source="{Binding Converter={StaticResource EnableDisableConverter}}" Height="25" Width="25" /> 
     </Button> 
    </DataTemplate> 
</data:DataGridTemplateColumn.CellTemplate> 

转换器正确返回基于状态的正确的图像。问题是我已经切换到MVVM模型,我的代码更改图像将不再工作。我以前是这样的:

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)); 
} 

期间更改的状态的命令,我试着养了变化包含对象的属性,但它并不反映它的UI。如果我对屏幕进行了硬刷新,则状态会正确更改。目前情况下有没有办法重新塑造图像?

回答

2

将图像的源代码绑定到某个bool Enabled属性,并且您的EnableDisableConverter可以在每次更改后对该值做出反应。

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> 

视图模型:

... 
public bool IsEnabled 
{ 
    get 
    { 
     return _isEnabled; 
    } 
    set 
    { 
     _isEnabled=value; 
     NotifyPropertyChanged("IsEnabled"); 
    } 
} 
... 

转换器:

public object Convert(object value, ...) 
{ 
    if((bool)value) 
     return uri of image1; 
    else 
     return uri of image2; 

} 

但我不知道是什么物体在网格中,什么是视图模型。可能有问题。重点是将IsEnabled属性正确绑定到网格中的这些对象。

+0

结束绑定到对象上的状态字段,并完美的工作。谢谢! – Josh 2011-03-10 15:35:54