在您的XAML:
<Button Focusable="False" Command="{Binding CmdClick}" Margin="0">
<Image Source="{Binding ImageSource,UpdateSourceTrigger=PropertyChanged} HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0">
</Image>
</Button>
在您的视图模型:
private Image buttonImage;
public Image ButtonImage
{
get
{
return buttonImage;
}
}
在你的ViewModel的构造函数或它的初始化
而且某处
private BitmapImage _ImageSource;
public BitmapImage ImageSource
{
get { return this._ImageSource; }
set { this._ImageSource = value; this.OnPropertyChanged("ImageSource"); }
}
private void OnPropertyChanged(string v)
{
// throw new NotImplementedException();
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(v));
}
public event PropertyChangedEventHandler PropertyChanged;
而在你的ViewModel的构造函数或它的初始化的地方:
string str = System.Environment.CurrentDirectory;
string imagePath = str + "\\Images\\something.png";
this.ImageSource = new BitmapImage(new Uri(imagePath, UriKind.Absolute));
OR:
string imagePath = "\\Images\\something.png";
this.ImageSource = new BitmapImage(new Uri(imagePath, UriKind.Relative));