我正在尝试通过C#将图像源绑定(bind)到我的XAML

这有效

<Image Source="images/man.jpg"></Image>

这不起作用
<Image Source="images/{Binding imagesource}"></Image>

其中imagesource是此xaml的C#文件中的字符串变量,并且被设置为等于“man.jpg”

最佳答案

这是在XAML中执行此操作的方法:

将此添加到 namespace :

xmlns:System="clr-namespace:System;assembly=mscorlib"

然后添加您的图像路径
<System:String x:Key="ImageRefresh">/Theme;component/Images/icon_refresh.png</System:String>
<System:String x:Key="ImageSearch">/Theme;component/Images/icon_search.png</System:String>

这就是你的用法
<Image Height="16" Source="{StaticResource ImageSearch}" Stretch="Uniform" Width="16"/>

这行得通,但是如果您在Blend中加载xaml样式,它将变得虚假。

无法将类型为“System.String”的对象应用于期望类型为“System.Windows.Media.ImageSource”的属性。

我还没有弄清楚如何用该Media.ImageSource替换System:String ...但是,嘿..它在Visual Studio中对我有用。

关于silverlight - 在XAML中绑定(bind)图像源,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3366166/

10-11 05:23
查看更多