【问题标题】:Why does binding image path to Image.Source does not work this way为什么将图像路径绑定到 Image.Source 不能以这种方式工作
【发布时间】:2015-08-04 21:46:07
【问题描述】:

我的图片路径声明如下:

public static string _edit_vector32 = "pack://application:,,,/Resources/Images/Icons/32/edit-vector2-32.png"; 

我尝试添加一个简单的属性,将 ImageSource 返回到我的 ViewModel,如下所示:

public ImageSource ClockImage
{
    get
    { 
        return new BitmapImage(new Uri(RuntimeSettings._clock24)) as ImageSource;
    }
 }

然后在 XAML 中绑定:

 <Image Source="{Binding ClockImage}"/>

为什么这不起作用,而:

 <Image Source="pack://application:,,,/Resources/Images/Icons/32/edit-vector2-32.png"/>

按预期工作?

【问题讨论】:

  • 第一种情况也应该有效,你确定你绑定到正确的属性吗?可能由于封闭元素的绑定,数据上下文不同?

标签: wpf image xaml binding properties


【解决方案1】:
<Image Source="pack://application:,,,/Resources/Images/Icons/32/edit-vector2-32.png"/>

之所以有效,是因为ImageSource 附加了一个值转换器 (ImageSourceConverter),它会自动将字符串转换为图像源。

第一种情况也应该有效(并且在我的测试项目中有效)。

【讨论】:

    猜你喜欢
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    相关资源
    最近更新 更多