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。 运行,就可以成功看到图片显示了。

时间: 2025-01-17 18:21:19

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

异常详细信息: System.Data.SqlClient.SqlException:用户 &#39;IIS APPPOOL\DefaultAppPool&#39; 登录失败解决办法

1.安全性---登录名---新建登录名 2.常规----搜索 3.添加SERVICE用户-- 4.服务器角色---勾上sysadmin: IIS中: 应用程序池---对应的程序池上右键---高级设置 进程模块---标识---选择NetworkService(与数据库中设置统一) 异常详细信息: System.Data.SqlClient.SqlException:用户 'IIS APPPOOL\DefaultAppPool' 登录失败解决办法

vs2008调用opencv2.4.9的imread()函数失败解决办法

这两天在看opencv的C++接口函数,刚开始就出现问题: 一个简单的显示图像的程序,就是运行不成功: #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <string> using std::string; using namespace cv; int main() { string str="E:\\test\\LENA.BMP";

MySQL安装失败解决办法

一..msi版的MySQL安装包在最后执行的时候到第三步就死掉了,直接未响应 这是因为以前在本机中安装过MySQL,卸载的时候没有卸载干净导致的,因为有的文件夹隐藏的很深. 1.卸载MySql相关组件: 2.删除MySQL的安装目录: 3.在注册表(regedit)查询mysql,全部删除: cmd -> regedit 1.HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Eventlog\Application\MySQL 目录 2.HKEY_

pureftp 读取目录列表失败解决办法

最近在使用pureftp的时候遇到了文件目录无法读取或者读取很慢的问题,同时服务器上配置了iptables防火墙规则,问题主要由此引起. 解决方法如下: 1.首先查看是否开启了20.21端口,如果没有请开启. 2.停止iptables:service iptables stop,再次连接,可以成功读取列表. 3.修改iptables-config配置文件: 打开:vim /etc/sysconfig/iptables-config 将IPTABLES_MODULES修改为:IPTABLES_MO

检索 COM 类工厂中 CLSID 为 {{10020200-E260-11CF-AE68-00AA004A34D5}} 的组件时失败解决办法

检索 COM 类工厂中 CLSID 为 {10020200-E260-11CF-AE68-00AA004A34D5} 的组件时失败,解决方法如下: 第一步:首先将msvcr71.dll,  SQLDMO.DLL, Resources/2052/sqldmo.rll,Resources/1033/sqldmo.rll 拷贝到C:/Program Files/Microsoft SQL Server/80/Tools/Binn目录. 下载SQLDMO文件 第二步:打开开始,在运行中输入 regsvr

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

原文:在ASP.net中的UpdatePanel,弹窗失败解决办法 最开始我用: Response.Write("<script>alert('和哈呵呵呵呵呵呵!')</script>"); 在没有UpdatePanel时,这个有效,能够正确弹出提示窗口. 后面,页面改进,加上局部刷新后,该方式失效了.探索出新的方式: ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(),&q

FreeBSD 下sac101.6a编译失败解决办法

由于FreeBSD和Linux下C的定义有些不同,可下载下面补丁修复编译问题. http://www.iris.washington.edu/pipermail/sac-help/attachments/20130910/7f30ed61/attachment.obj FreeBSD 下sac101.6a编译失败解决办法,布布扣,bubuko.com

SVN cleanup操作反复失败解决办法 (转载)

SVN cleanup操作反复失败解决办法 2014-11-21 11:12:24 标签:SVN cleanup sqlite3 work_queue 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://flyingcat2013.blog.51cto.com/7061638/1580692 今天在更新项目的时候遇到一个问题,按惯例要cleanup才能重新更新.但是很不幸,在cleanup的时候又遇到了问题! 1    svn c

VS2012 加载项目失败解决办法

项目文件被卸载时: 一般解决办法为 1.编辑 ****.csproj文件 2.注释:<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 3.重新加载即可 VS2012 加载项目失败解决办法