【问题标题】:WPF: Image binding with property of DataContext in XAMLWPF:在 XAML 中具有 DataContext 属性的图像绑定
【发布时间】:2012-11-17 09:16:29
【问题描述】:

我尝试根据such examplesImage 设置绑定。我的 XAML:

<Image x:Name="imgPhoto" x:FieldModifier="public" HorizontalAlignment="Stretch" 
     VerticalAlignment="Stretch">
    <Image.Source>
        <BitmapImage UriSource="{Binding Path=Photo, Mode=TwoWay}"/>
    </Image.Source>
</Image>

但我在窗口的构造函数中遇到异常(如下所示)。为什么会这样?

System.Windows.Markup.XamlParseException 发生 HResult =-2146233087 消息 =““System.Windows.Media.Imaging.BitmapImage”的初始化导致异常。”:行 编号“57”和第“34”行中的一个行项目。

PhotoEmploye 项目的属性,它将是 DataContext 的来源。它的属性将是一个"pack://application:,,,/myImages/male.png"string 值。我将为我的Window 实例设置它的属性。但是在我通过我的值初始化这个属性之前,我在Window 的构造函数中遇到了这样的异常。我的错在哪里?

【问题讨论】:

    标签: wpf xaml binding


    【解决方案1】:

    您可以直接绑定到Image.Source 您的Photo 属性:

    <Image Source="{Binding Path=Photo}" />
    

    Photo 属性应该如下所示:

    "/YourProjectName;component/myImages/myPhoto.png";
    

    当然 png 文件应该将 Build Action 设置为 Resource

    在 msdn 上你有很好的文章,下面是它的链接。

    Pack URIs in WPF

    【讨论】:

      猜你喜欢
      • 2013-12-09
      • 2023-01-16
      • 2016-10-28
      • 2016-03-10
      • 1970-01-01
      • 1970-01-01
      • 2017-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多