我在将图像绑定到我的viewmodel时遇到了一些麻烦。我终于摆脱了XamlParseException,但图像不出来。我甚至硬编码在ViewModel中的图像。有人可以看到我做错了什么吗?在WPF MVVM中绑定图像
查看:
<Image HorizontalAlignment="Left" Margin="0,0,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Bottom" Grid.Row="8" Width="200" Grid.ColumnSpan="2" >
<Image.Source>
<BitmapImage DecodePixelWidth="200" UriSource="{Binding Path=DisplayedImage, Mode=TwoWay}" />
</Image.Source>
视图模型:
string _DisplayedImagePath = @"C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg";//string.Empty;
int _DisplayedImageIndex;
BitmapImage _DisplayedImage = null;
public BitmapImage DisplayedImage
{
get
{
_DisplayedImage = new BitmapImage();
if (!string.IsNullOrEmpty(_DisplayedImagePath))
{
_Rail1DisplayedImage.BeginInit();
_Rail1DisplayedImage.CacheOption = BitmapCacheOption.OnLoad;
_Rail1DisplayedImage.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
_Rail1DisplayedImage.UriSource = new Uri(_DisplayedImagePath);
_Rail1DisplayedImage.DecodePixelWidth = 200;
_Rail1DisplayedImage.EndInit();
}
return _Rail1DisplayedImage;
}
set
{
_Rail1DisplayedImage = value;
OnPropertyChanged("DisplayedImage");
}
}
感谢您的响应。我试过这个代码,在DisplayedImage中放置了一个断点,它肯定被访问但没有图像。 – kurgaan
对不起,这意味着表单上没有图像。如果我将路径直接放在XAML中,图像就会显示出来。 – kurgaan
是的,抱歉,我没有正确地看到你的财产......这比这更容易。看我的编辑。 – Sheridan