2014-02-14 70 views
30

我在将图像绑定到我的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"); 
     } 
    } 

回答

56

显示在WPF的Image较容易得多。试试这个:

<Image Source="{Binding DisplayedImagePath}" HorizontalAlignment="Left" 
    Margin="0,0,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Bottom" 
    Grid.Row="8" Width="200" Grid.ColumnSpan="2" /> 

,酒店还可以只是一个string

public string DisplayedImage 
{ 
    get { return @"C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg"; } 
} 

虽然你真的应该添加图片到你的项目的根命名Images文件夹,并设置其生成操作资源属性窗口在Visual Studio中...然后您可以使用以下格式访问它们:

public string DisplayedImage 
{ 
    get { return "/AssemblyName;component/Images/ImageName.jpg"; } 
} 

UPDATE >>>

作为最后一个提示......如果你曾经有一个问题,如预期控制不能正常工作,只需键入“WPF”,该控件的名称,然后搜索引擎中的'class'这个词。在这种情况下,你应该输入'WPF Image Class'。最重要的结果将始终是MSDN,如果你点击链接,你会发现所有关于该控件的信息,而且大多数页面都有代码示例。


更新2 >>>

如果您是从链接到MSDN的例子,它不工作,那么你的问题是Image控制。使用string属性,我建议,试试这个:

<StackPanel> 
    <Image Source="{Binding DisplayedImagePath}" /> 
    <TextBlock Text="{Binding DisplayedImagePath}" /> 
</StackPanel> 

如果你不能看到TextBlock文件路径,那么你可能还没有设置您的DataContext到您的视图模型的实例。如果你可以看到文本,那么问题出在你的文件路径上。


UPDATE 3 >>>

在.NET 4,以上Image.Source值会工作。如果我

<Image Source="pack://application:,,,/AssemblyName;component/Images/image_to_use.png"> 
+0

感谢您的响应。我试过这个代码,在DisplayedImage中放置了一个断点,它肯定被访问但没有图像。 – kurgaan

+0

对不起,这意味着表单上没有图像。如果我将路径直接放在XAML中,图像就会显示出来。 – kurgaan

+0

是的,抱歉,我没有正确地看到你的财产......这比这更容易。看我的编辑。 – Sheridan

0

@Sheridan THX ..:不过,微软取得了.NET 4.5,打破许多不同的事情,所以在.NET 4.5,你需要使用完整pack路径类似这样的一些可怕的变化尝试使用“DisplayedImagePath”两侧的示例,它可以像绝对路径一样工作。

至于相对路径,这是我总是如何连接相对路径,我首先包括子目录(!)和图像文件在我的项目中..然后我用〜字符来表示bin路径。 。

public string DisplayedImagePath 
    { 
     get { return @"~\..\images\osc.png"; } 
    } 

此测试,请参阅下面我的解决方案资源管理器在VS2015 ..

example of image binding in VS2015

注意:如果你想有一个Click事件,使用Button tag around the image

<Button Click="image_Click" Width="128" Height="128" Grid.Row="2" VerticalAlignment="Top" HorizontalAlignment="Left"> 
 
    <Image x:Name="image" Source="{Binding DisplayedImagePath}" Margin="0,0,0,0" /> 
 
</Button>