【问题标题】:How to binding Image from view-model in another dll?如何将来自视图模型的图像绑定到另一个 dll 中?
【发布时间】:2013-07-12 16:34:19
【问题描述】:

我有一个 WPF Window 项目和一个带有它的图像控件。 我编写了另一个 dll 项目,这将是窗口的视图模型。 这个 dll 项目包含一个名为“h.png”的 png 文件,我将把它作为源绑定到 WPF Window 项目。 我设置了图像绑定,但它不起作用, 谁能告诉我如何绑定来自另一个 dll 项目的图像源?

我的 C# 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;

namespace vm
{
    public class VMClass
    {
        BitmapImage img;

        public BitmapImage Img
        {
            get { return img; }
            set { img = value; }
        }
        public VMClass()
        {
            img = new BitmapImage(new Uri("h.png", UriKind.RelativeOrAbsolute));


        }
    }
}

我的 xaml:

<Window x:Class="ImageBindingApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:vm;assembly=vm"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <vm:VMClass/>
    </Window.DataContext>
    <Grid>
        <Border BorderThickness="2" BorderBrush="Red">
            <Image Source="{Binding Img}"/>
        </Border>
    </Grid>
</Window>

【问题讨论】:

    标签: c# wpf image binding


    【解决方案1】:

    1) 确保 h 图像已作为资源添加(您可以在图片属性中查看):

    2) 将您的 uri 字符串 (msdn) 更改为:

     new BitmapImage(new Uri("/vm;component/h.png", UriKind.RelativeOrAbsolute));
    

    【讨论】:

      【解决方案2】:

      首先,您需要检查您为图像选择的构建操作类型(页面、资源、嵌入资源、内容...)。您可以通过在 Visual Studio 中检查图像的属性窗口来执行此操作。
      然后检查如何为您的图像定义正确的 uri 路径。这取决于您的文件的位置。详情请参考Pack URIs in WPF

      【讨论】:

        猜你喜欢
        • 2023-03-07
        • 2014-07-28
        • 2014-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-02
        相关资源
        最近更新 更多