我正在尝试通过 c# 将图像源绑定(bind)到我的 XAML
这有效
<Image Source="images/man.jpg"></Image>
这不起作用
<Image Source="images/{Binding imagesource}"></Image>
其中 imagesource 是此 xaml 的 c# 文件中的字符串变量,并且被设置为等于“man.jpg”
最佳答案
这是一种在 XAML 中执行此操作的方法:
将其添加到命名空间:
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/