在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. 广告基本知识-ROI分解

    任何一个在线广告系统,都面临ROI的问题,对于Invest,我们先不考虑,因为对于流量有多种方式可以买回,也无法优化(当然在RTB的时候是可以优化的).Return是主要优化的方向,Return=点击 ...

  2. RDLC(Reportview)报表

    你还可以再诡异点吗——SQL日志文件不断增长   前言 今天算是遇到了一个罕见的案例. SQL日志文件不断增长的各种实例不用多说,园子里有很多牛人有过介绍,如果我再阐述这些陈谷子芝麻,想必已会被无数次 ...

  3. LINUX安装SVN+添加自动同步+远程下载最新代码

    LINUX安装SVN+添加自动同步+远程下载最新代码---------------------1. 新建一个用户:svnroot ,以下操作非特别说明皆为root用户操作--------------- ...

  4. c# AutoResetEvent和ManualResetEvent

    网上有很多AutoResetEvent和ManualResetEvent的详细介绍,在这里不做过多详细的解释,写下自己的一点心得留作备忘. AutoResetEvent和ManualResetEven ...

  5. asp.net中的路由系统

    ASP.NET MVC重写了ASP.NET管道HttpModule和处理程序HttpHandler.MVC自定义了MvcHandler实现了Controller的激活和Action的执行.但是在请求到 ...

  6. 设置代码ios 6添加日历事件No calendar has been set.错误

    查了好多资料,发明还是不全,干脆自己整理吧,至少保证在我的做法正确的,以免误导读者,也是给自己做个记录吧! 在iOS 6做日历事件添加时,来来回回报错,No calendar has been set ...

  7. android电源“有毒”移动电源Android版的设计及其实现

    工作之余抽点时间出来写写博文,希望对新接触的朋友有帮助.今天在这里和大家一起学习一下android电源 报道http://www.cnbeta.com/articles/239726.htm称:安全研 ...

  8. 如何在ASP.NET Core应用中实现与第三方IoC/DI框架的整合?

    我们知道整个ASP.NET Core建立在以ServiceCollection/ServiceProvider为核心的DI框架上,它甚至提供了扩展点使我们可以与第三方DI框架进行整合.对此比较了解的读 ...

  9. ubuntu 14.04中Elasticsearch 2.3 中 Nginx 权限认证

    前言:本文实现了nginx简单保护elasticsearch,类似的保护也可以采用elasticsearch 官方插件shield 一.准备密码 1.确认htpasswd是否已经安装 which ht ...

  10. Swift之父Chris Lattner将从Apple离职,加入特斯拉

        1月10日,Swift编程语言之父 Chris Lattner 在 swift-evolution 邮件列表中宣布,他将于本月底离开 Apple,Ted Kremenek 将接替他成为 Swi ...