【问题标题】:Binding to Image Property绑定到图像属性
【发布时间】:2011-03-11 03:44:22
【问题描述】:

假设我有以下虚拟类:

public class Foo
    {
        public Image MyImage
        {
            get;
            set;
        }
    }

我在一些 XAML 中有以下内容

<Image Source="{Binding Foo.MyImage}"/>

如果我理解正确,这是行不通的,因为 Source 期望 MyImage 存储位置的 URI 字符串值,但在这种情况下没有 URI,因为 MyImage 在内存中。

我怎样才能使上述工作?

编辑:

这就是 MyImage 的创建方式:

private void CreateBarCode(string bcValue)
    {
        Code128 bc128 = new Code128();
        bc128.HumanReadable = Code128.TextWhere.Below;
        this.MyImage = new Bitmap(bc128.Generate(bcValue)); 
    }

通过一个接受字符串值并返回位图格式的条形码的方法。

【问题讨论】:

标签: wpf image data-binding


【解决方案1】:

Image 控件上的Source 属性是ImageSource 类型。您的MyImage 属性具有可通过MyImage.Source 访问的属性之一。所以你的 xaml 看起来像

<Image DataContext={Binding Path=_anInstanceOfClassFoo} Source="{Binding Path=MyImage.Source}"/>

通常您不会直接设置 DataContext,而是让它从父控件继承。我这样做只是因为在您的示例中,您的绑定直接引用 Foo 类而不是实例

【讨论】:

  • 所以即使 MyImage 是通过方法创建的,它仍然有来源?
  • Source 只是 System.Windows.Controls.Image 类上 ImageSource 类型的属性。您目前如何生成 MyImage 对象?
猜你喜欢
  • 1970-01-01
  • 2010-12-02
  • 1970-01-01
  • 1970-01-01
  • 2016-03-23
  • 2014-06-18
  • 2016-10-21
  • 2021-07-23
  • 2016-10-01
相关资源
最近更新 更多