更改基于绑定的按钮背景图像

问题描述:

我试图从按钮中获取背景图像以基于MVVM链接对象中的值进行更改。更改基于绑定的按钮背景图像

该按钮的默认样式图像1,

当物体已被处理成功,该属性设置为“成功”和按钮图像应更改为镜像2

然而当对象不是招没有被处理成功,该属性设置为“错误:”,并且按钮图像应该变成image3

我已经尝试绑定到表示文件位置的字符串(直接在ImageBrush上输入相同的字符串属性ImageSource在Button.Background标记中。

也返回一个开放的,以该文件不工作

试图利用与触发器二传手标签,使用

的XAML休息时,并试图从它创建的BitmapImage与内的额外属性对象也不起作用。

这是应用程序中剩下的唯一东西。有人有一些建议

+0

你能展示一些代码吗? – Romasz 2014-09-25 09:16:33

我现在发现了一个可行的解决方案。通过从该按钮的背景,并加入以下内容:

 <Button.Content> 
     <StackPanel Orientation="Vertical"> 
      <Image Source="{Binding Path=Background}" Stretch="Fill" Height="100" Width="400" /> 
      <TextBlock Margin="0,-130,0,0" Height="30" Width="400" HorizontalAlignment="Center" 
       TextAlignment="Center" VerticalAlignment="Center" Text="{Binding Path=Title}" 
       Foreground="White" FontFamily="/Assets/futura-md.ttf#Futura Md BT"/> 
     </StackPanel> 
     </Button.Content> 

具有背景定义如下:

public BitmapImage Background 
    { 
     get 
     { 
      return new BitmapImage(new Uri(string.Format("/Assets/knop_{0}.png",buttoncolor), UriKind.Relative)); 
     } 
    } 

    public string ButtonImage 
    { 
     get { return buttonimage; } 
     set 
     { 
      buttonimage = value; 
      RaisePropertyChanged("ButtonImage"); 
      RaisePropertyChanged("Background"); 
     } 
    } 

现在按钮“背景”将被改变时的值ButtonImage已更改。

这不完全是我所希望的,但它的工作。