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失败,后来从网上找打了资料解决了.解决办法:一:打开你存放虚拟机系统文件的文件夹,注意,是系统文件,不是 ...
随机推荐
- 淘宝ued - 前端智勇大闯关(第三季)答案(更新)
淘宝ued - 前端智勇大闯关(第三季)答案(更新) 下午在微博上看到了淘宝智勇大闯关第三季的信息,感觉挺有意思的,于是就尝试做了下.附上题目地址: http://ued.campus.alibaba ...
- C# 对象池的实现
C# 对象池的实现 对象池服务可以减少从头创建每个对象的系统开销.在激活对象时,它从池中提取.在停用对象时,它放回池中,等待下一个请求.我们来看下主线程中,如何与对象池打交道: static void ...
- 一种基于自定义代码的asp.net网站首页根据IP自动跳转指定页面的方法!
一种基于自定义代码的asp.net网站首页根据IP自动跳转指定页面的方法! 对于大中型网站,为了增强用户体验,往往需要根据不同城市站点的用户推送或展现相应个性化的内容,如对于一些大型门户网站的新闻会有 ...
- SharePoint 2013/2010 中的日历重合 (Calendars Overlay)
本文介绍 SharePoint 2013/2010 中的日历重合 (Calendars Overlay). 日历重合 (Calendars Overlay)的用途就是将 不多于10个日历或日历视图聚集 ...
- VS2012编译LibZip库
LibZip库是解压缩zip文件的C库. 中文的网站目前还没有找到相关的编译方法,找了一篇英文文档加上自己的实验修改了一下: 编译步骤如下: 首先先下载CMake,我下载的是2.8版本. libzip ...
- C++ 中内存分配和回收
void Allocate(char* &p,int size) { p = (char*)malloc(size); } void Test(void) { char *str = NULL ...
- OleDbCommand cmd.Parameters.AddWithValue 添加参数时需要按照存储过程参数的顺序加入
在使用存储过程时,参数出入的顺序要一致.
- 通过Jasmine和Guard自动测试JavaScript
原文标题:Autotesting JavaScript with Jasmine and Guard 原文地址:http://edspencer.net/2013/06/15/autotesting- ...
- Elasticsearch 2.3 (ELK)Geo_point绘图、日志Date时间获取实例
前言:本文源于天天是雾霾新闻,我想利用kibana画一下一线城市雾霾图,希望对想利用经纬度在kibana绘图和获取日志本身时间绘图的同学有所帮助.有什么疑问或者纠错,可以给我发邮件 一.数据准备 为了 ...
- .Net多线程编程—同步机制
1.简介 新的轻量级同步原语:Barrier,CountdownEvent,ManualResetEventSlim,SemaphoreSlim,SpinLock,SpinWait.轻量级同步原语只能 ...