【发布时间】: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>
【问题讨论】: