WPF Image Binding Uri Source 失败解决办法
在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 失败解决办法的更多相关文章
- Mysql文件太大导入失败解决办法总结
Mysql文件太大导入失败解决办法总结 在使用phpmyadmin导入数据库的时候可能会碰到由于数据库文件太大而无法导入的问题! 英文提示如下:File exceeds the maximum all ...
- 在ASP.net中的UpdatePanel,弹窗失败解决办法
原文:在ASP.net中的UpdatePanel,弹窗失败解决办法 最开始我用: Response.Write("<script>alert('和哈呵呵呵呵呵呵!')</s ...
- myeclipse2017破解失败解决办法
最近,笔者安装的myeclipse2017破解出了问题,破解本来是很简单的事,就是几步而已,但是一直出问题,现在安利一波myeclipse2017版破解失败解决办法.诸如下图:()因为笔者已经破解好了 ...
- python 安装pytorch 及 安装失败解决办法
python 安装pytorch 及 安装失败解决办法 [转] pytorch安装失败解决办法 [转] 一分钟在win10终端成功安装pytorch pytorch 的安装方法有2种,一种是pip安装 ...
- c++调用动态库失败解决办法
c++调用动态库失败解决办法 之前写好的程序今天早上过来发现在服务器上出错了,于是就各种查问题,整整一个早上外加下午两个小时都在查这个问题,最终被我找到了问题: 在程序中我发现LoadLibrary( ...
- Hive中将文件加载到数据库表失败解决办法
Hive中将文件加载到数据库表失败解决办法(hive创建表失败) 遇到的问题: FAILED: Execution Error, return code 1 from org.apache.hadoo ...
- SVN cleanup操作反复失败解决办法
今天在更新项目的时候遇到一个问题,按惯例要cleanup才能重新更新.但是很不幸,在cleanup的时候又遇到了问题! 1 svn cleanup failed–previous operati ...
- adobe photoshop cc 2014 安装失败 解决办法之一
首先安装失败会有提示 首先贴下错误信息 Exit Code: 34 Please see specific errors below for troubleshooting. For example, ...
- 虚拟机锁定文件失败,开启模块snapshot失败解决办法
今天由于没有正常关闭虚拟机,导致出现打开虚拟机提示:锁定文件失败 虚拟机开启模块snapshot失败,后来从网上找打了资料解决了.解决办法:一:打开你存放虚拟机系统文件的文件夹,注意,是系统文件,不是 ...
随机推荐
- HttpStack及其实现类
HttpStack及其实现类 前两篇已经对网络请求流程已经梳理了个大概,这次我们着重看一下HttpStack和它的其实现类.我们之前在Network篇讲过它仅有一个实现类,而今天我们讲的HttpSta ...
- service structure flowchart [mobile to server via HTTP RESTful API]
Modern flowchart for mobile, server, and etc.. communication This has something to do with these sou ...
- 用Linux命令行实现删除和复制指定类型的文件
(一)Linux 删除当前目录及子目录中所有某种类型的文件 方法1 : 此方法不能处理目录中带空格的那些. rm -rf `find . -name "*.example"` Li ...
- 分享一个javascript alert精简框架
如果你不喜欢浏览器自带的alert你可以尝试总共不超过10KB somke js 下载地址:http://smoke-js.com/ 使用方法 somke.alert("hello wor ...
- linux下如何使用USB存储设备
如何在Linux环境中使用USB接口的 存储 设备?这是各大电脑论坛上出现得比较多的一个问题,同此可见这也是摆在许多电脑玩家面前的一道难题. 本文就为您提供一套完美的解决方案,通过下面的方法,您仅可以 ...
- Linux进程分配内存的两种方式--brk() 和mmap()
如何查看进程发生缺页中断的次数? 用ps -o majflt,minflt -C program命令查看. majflt代表major fault,中文名叫大错误,minflt代表minor faul ...
- oracle表空间表分区详解及oracle表分区查询使用方法(转+整理)
欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...
- [DL学习笔记]从人工神经网络到卷积神经网络_3_使用tensorflow搭建CNN来分类not_MNIST数据(有一些问题)
3:用tensorflow搭个神经网络出来 为什么用tensorflow呢,应为谷歌是亲爹啊,虽然有些人说caffe更适合图像啊mxnet效率更高等等,但爸爸就是爸爸,Android都能那么火,一个道 ...
- [oc] instancetype vs id for Objective-C 【转】
原贴地址:http://blog.csdn.net/lyy_whg/article/details/12846055 http://www.iwangke.me/2013/01/06/instance ...
- MyEclipse导入Maven项目pom文件第一行报错,运行Tomcat报Log4j错误--解决方法
问题描述: 前一段时间电脑第一次导入Maven项目,又是pom文件错,改好后又是运行Tomcat报Log4j错误,一直倒腾了近一个月程序才成功跑起来,太不容易. 也上网查了很长时间,没一个方法能解决我 ...