在ListView 的ListItem里动态绑定Image. 首先代码写的是没有问题的。但最后运行却无法显示图片。先看代码:

1. XAML部分 代码如下:

<ListView x:Name="m_DestinationListView" HorizontalAlignment="Left"   ItemsSource="{Binding}" Width="785" Height="230"  VerticalAlignment="Top">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Width="785" Height="120">
<Border BorderBrush="Black" BorderThickness="0 1 0 0"></Border>
<Grid Width="785" Visibility="{Binding Path=IsItem}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"></ColumnDefinition>
<ColumnDefinition Width="40*"></ColumnDefinition>
<ColumnDefinition Width="55*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30*"></RowDefinition>
<RowDefinition Height="35*"></RowDefinition>
<RowDefinition Height="35*"></RowDefinition>
</Grid.RowDefinitions> <Image Source="{Binding DestIcon}" Width="50" Height="50" Stretch="Fill"/>
<TextBlock Text="{Binding DestName}" Grid.Row="0" Grid.Column="1" Style="{StaticResource BoldTextStyle}" FontSize="18"></TextBlock>
<TextBlock Text="{Binding Path=ApptBeginTime}" Grid.Row="0" Grid.Column="2" Style="{StaticResource BoldTextStyle}" FontSize="18"></TextBlock>
<TextBlock Text="{Binding Path=Address}" Grid.Row="1" Grid.Column="1"></TextBlock>
<TextBlock Text="{Binding Path=SecondAddressLine}" Grid.Row="2" Grid.Column="1"></TextBlock>
</Grid>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

2. C#代码如下:

A. ListViewItem 结点的定义:

namespace TripManagerWpfUI
{
public class TripPlanMockGoose : DependencyObject
{
public TripPlanMockGoose()
{
} public string DestName
{
get { return (string)GetValue(DestNameProperty); }
set { SetValue(DestNameProperty, value); }
}
// Using a DependencyProperty as the backing store for Name. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DestNameProperty =
DependencyProperty.Register("DestName", typeof(string), typeof(TripPlanMockGoose), new PropertyMetadata("")); public string ApptBeginTime
{
get { return (string)GetValue(ApptBeginTimeProperty); }
set { SetValue(ApptBeginTimeProperty, value); }
}
// Using a DependencyProperty as the backing store for Name. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ApptBeginTimeProperty =
DependencyProperty.Register("ApptBeginTime", typeof(string), typeof(TripPlanMockGoose), new PropertyMetadata("")); public string SecondAddressLine
{
get { return (string)GetValue(SecondAddressLineProperty); }
set { SetValue(SecondAddressLineProperty, value); }
}
// Using a DependencyProperty as the backing store for Name. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SecondAddressLineProperty =
DependencyProperty.Register("SecondAddressLine", typeof(string), typeof(TripPlanMockGoose), new PropertyMetadata("")); public string Address
{
get { return (string)GetValue(AddressProperty); }
set { SetValue(AddressProperty, value); }
}
// Using a DependencyProperty as the backing store for Name. This enables animation, styling, binding, etc...
public static readonly DependencyProperty AddressProperty =
DependencyProperty.Register("Address", typeof(string), typeof(TripPlanMockGoose), new PropertyMetadata("")); //此处为图片的及其依赖属性的定义:
/// <summary>
/// Gets or sets the icon of the item.
/// </summary>
public BitmapImage DestIcon
{
get { return (BitmapImage)GetValue(DestIconProperty); }
set { SetValue(DestIconProperty, value); }
}
// Using a DependencyProperty as the backing store for Name. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DestIconProperty =
DependencyProperty.Register("DestIcon", typeof(BitmapImage), typeof(TripPlanMockGoose), null);
}
}

B: 结点的绑定:

private void UpdateListView()
{
TripPlan tp;
tp = TripManagerApp.Root.ActiveTripPlan;
m_ListView = new List<TripPlanMockGoose>(); foreach (Destination d in tp.GetDestinationList())
{
TripPlanMockGoose node = new TripPlanMockGoose(); node.DestName = d.destName;
node.ApptBeginTime = d.apptBeginTime.ToString();
node.Address = d.address;
node.SecondAddressLine = d.SecondAddressLine;//图片绑定
node.DestIcon = GetItemIcon(d); m_ListView.Add(node);
} //设置ListView的ItemSource
m_DestinationListView.ItemsSource = m_ListView;
} private BitmapImage GetItemIcon(Destination dest)
{
BitmapImage icon = new BitmapImage();
icon.BeginInit();
switch (dest.destinationType)
{
case Destination.validDestinationTypes.origin:
icon.UriSource = new Uri(
//注意此两种Uri的表达方式都可以
"pack://application:,,,/TripManagerWpfUI;component/Resources/driverworkflow_icon_origin.png",
UriKind.RelativeOrAbsolute);
break; case Destination.validDestinationTypes.dropoffRelay:
icon.UriSource = new Uri(
"/TripManagerWpfUI;component/Resources/driverworkflow_icon_dropOffRelay.png",
UriKind.RelativeOrAbsolute);
break; case Destination.validDestinationTypes.terminalStart:
default:
icon.UriSource = new Uri(
"/TripManagerWpfUI;component/Resources/driverworkflow_icon_origin.png",
UriKind.Relative);
break; }
icon.EndInit();
return icon;
}

问题:代码和路径都是没有错的,但最后运行图片却显示不出来。经过仔细排查,问题出在 图片资源文件的Build Action 类型。

在右键单击图片Properties. 看Build Action的类型, 会发现是Content. 这时把其改为Resource, 再rebuild。 运行,就可以成功看到图片显示了。

WPF Image Binding Uri Source 失败解决办法的更多相关文章

  1. Mysql文件太大导入失败解决办法总结

    Mysql文件太大导入失败解决办法总结 在使用phpmyadmin导入数据库的时候可能会碰到由于数据库文件太大而无法导入的问题! 英文提示如下:File exceeds the maximum all ...

  2. 在ASP.net中的UpdatePanel,弹窗失败解决办法

    原文:在ASP.net中的UpdatePanel,弹窗失败解决办法 最开始我用: Response.Write("<script>alert('和哈呵呵呵呵呵呵!')</s ...

  3. myeclipse2017破解失败解决办法

    最近,笔者安装的myeclipse2017破解出了问题,破解本来是很简单的事,就是几步而已,但是一直出问题,现在安利一波myeclipse2017版破解失败解决办法.诸如下图:()因为笔者已经破解好了 ...

  4. python 安装pytorch 及 安装失败解决办法

    python 安装pytorch 及 安装失败解决办法 [转] pytorch安装失败解决办法 [转] 一分钟在win10终端成功安装pytorch pytorch 的安装方法有2种,一种是pip安装 ...

  5. c++调用动态库失败解决办法

    c++调用动态库失败解决办法 之前写好的程序今天早上过来发现在服务器上出错了,于是就各种查问题,整整一个早上外加下午两个小时都在查这个问题,最终被我找到了问题: 在程序中我发现LoadLibrary( ...

  6. Hive中将文件加载到数据库表失败解决办法

    Hive中将文件加载到数据库表失败解决办法(hive创建表失败) 遇到的问题: FAILED: Execution Error, return code 1 from org.apache.hadoo ...

  7. SVN cleanup操作反复失败解决办法

    今天在更新项目的时候遇到一个问题,按惯例要cleanup才能重新更新.但是很不幸,在cleanup的时候又遇到了问题! 1    svn cleanup failed–previous operati ...

  8. adobe photoshop cc 2014 安装失败 解决办法之一

    首先安装失败会有提示 首先贴下错误信息 Exit Code: 34 Please see specific errors below for troubleshooting. For example, ...

  9. 虚拟机锁定文件失败,开启模块snapshot失败解决办法

    今天由于没有正常关闭虚拟机,导致出现打开虚拟机提示:锁定文件失败 虚拟机开启模块snapshot失败,后来从网上找打了资料解决了.解决办法:一:打开你存放虚拟机系统文件的文件夹,注意,是系统文件,不是 ...

随机推荐

  1. mmDeferred

    前端异步解决方案——mmDeferred Deferred是前端解决异步操作的一种编程范式,后来出现的Promise规范更是让其普适性大大提高.不过Promise规范也存在分岐.现在最流行的是Prom ...

  2. 用Winrar批量解压缩有密码文件方法,只需输入一次密码

    老王上传的文件多是RAR压缩格式的, 每个系列下载完,都20多集,解压缩的时候要一个一个的输入密码,太浪费时间. 1) 把下载的需要解压缩的文件统一放到一个文件夹下. 2) 启动winrar程序 (从 ...

  3. .NET对象占内存多少

    .NET对象占内存多少 一直有一个小小的疑惑——.NET一个对象或者一个集合占多少内存?有没有很快速的方法获取,而不是简单的估计分析对象大小? 查了MSDN,和一些其他人的分析,得到解决是托管代码对象 ...

  4. PHP5.3 里面数组的的实现方式

    typedef struct _Bucket { char *key; void *value; struct _Bucket *next; } Bucket; typedef struct _Has ...

  5. bin文件和elf文件

    ELF文件格式是一个开放标准,各种UNIX系统的可执行文件都采用ELF格式,它有三种不同的类型: 可重定位的目标文件(Relocatable,或者Object File) 可执行文件(Executab ...

  6. poj 3897 Maze Stretching 二分+A*搜索

    题意,给你一个l,和一个地图,让你从起点走到终点,使得路程刚好等于l. 你可以选择一个系数,把纵向的地图拉伸或收缩,比如你选择系数0.5,也就是说现在上下走一步消耗0.5的距离,如果选择系数3,也就是 ...

  7. Android Device Chooser中显示Target unknown解决方法

    手机插在电脑上准备调试程序来着,通过eclipse运行时,弹出的Android Device Chooser中显示设备名是?????,Target未知,无法继续运行. 可以通过以下步骤解决(Ubunt ...

  8. C#中数组,ArrayList与List对象的区别

    在C#中,当我们想要存储一组对象的时候,就会想到用数组,ArrayList,List这三个对象了.那么这三者到底有什么样的区别呢? 我们先来了解一下数组,因为数组在C#中是最早出现的. 数组 数组有很 ...

  9. JVM执行引擎的执行过程

    摘自深入分析java web技术内幕

  10. [转]HTTP请求模型和头信息参考

    [转]HTTP请求模型和头信息参考 参考: http://blog.csdn.net/baggio785/archive/2006/04/13/661410.aspx模型: http://blog.c ...