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失败,后来从网上找打了资料解决了.解决办法:一:打开你存放虚拟机系统文件的文件夹,注意,是系统文件,不是 ...
随机推荐
- Nginx学习之十四-GDB调试Nginx初试
本文的测试环境: Win7+虚拟机VMWareVMware-workstation-full-7.1.4-385536+Ubuntu12.04 Nginx-1.4.0 要想有效的研究Nginx源码,必 ...
- [置顶] ZK(The leading enterprise Ajax framework)入门指南
1. Why ZK JavaEE领域从来就不缺少Framework尤其是Web Framework,光是比较流行的就有:SpringMVC.Struts2.JSF系列…… 其它不怎么流行的.小众的.非 ...
- android 豆瓣客户端 视频
链接如下:http://download.csdn.net/detail/jltxgcy/5667337
- 导入导出csv文件
在工作中需要把csv文件数据导入数据库或者把数据库数据导出为csv文件.以下是我的简单的实现. <?php class csv { public $db_connection; public $ ...
- for循环和while循环
for循环和while循环 --道心 for循环 name1_list=['daoxin','wuxin','zhixin']for ele in name1_list: #找到"wuxin ...
- Python第一天——入门Python(1)数据定义
数据类型: 什么是数据? 在计算机科学中,数据是指所有能输入到计算机并被计算机程序处理的符号的介质的总称,是用于输入电子计算机进行处理,具有一定意义的数字字母.符号和模拟量等的统称.现在计算机存储和处 ...
- Linux之初体验
预备作业03--我的Linux初体验 学习基于VirtualBox虚拟机安装Ubuntu图文教程在自己笔记本上安装Linux操作系统 一开始以为这个项目很简单,以往也在自己的笔记本上看教程安装过软件, ...
- CodeForces 645C Enduring Exodus
枚举,三分. 首先,这$n+1$个人一定是连续的放在一起的.可以枚举每一个起点$L$,然后就是在$[L,R]$中找到一个位置$p$,使得$p4最优,因为越往两边靠,距离就越大,在中间某位置取到最优解, ...
- 在server2012安装tfs遇到的问题:KB2919355
参考资料:http://blog.csdn.net/wo_984633714/article/details/52869851 安装tfs2015的时候,提示需要安装KB2919355的更新.然后我就 ...
- MVC-工作原理
ASP.NET MVC的原理,其实就是使用HttpModule和HttpHandler将用户的请求拦截,按照设定的路由规则解释到相应的控制器和Action,加以执行.Module是一个比较宏观一点的概 ...