wpf - 将 ImageSource 对象直接绑定(bind)到 XAML/WPF 中的图像源?

标签 wpf mvvm binding imagesource

我有以下 XAML 代码:

<ListBox ItemsSource="{Binding Languages}">  
    <ListBox.ItemTemplate>  
        <DataTemplate>  
            <StackPanel>  
                <Image Source="{Binding LanguageIcon}" />  
                <Label Content="{Binding LanguageName}" />  
            </StackPanel>  
        </DataTemplate>  
    </ListBox.ItemTemplate>  
</ListBox>  

在模型类中:

class Language {  
    public string LanguageName;  
    public ImageSource LanguageIcon;  
}  

我的模型 View 类包含:

public List<Language> Languages;

里面装满了:

Languages.Add(new Language("A",new BitmapImage(new Uri("Resources/a.ico",
    UriKind.Relative))));  
Languages.Add(new Language("B",new BitmapImage(new Uri("Resources/b.ico",
    UriKind.Relative))));  

当我运行项目时,我所有的语言名称都显示在列表中,但没有显示图标...为什么会发生这种情况,我如何确保显示我的图标? (我确信资源确实存在,因为 BitmapImages 不会抛出错误)

最佳答案

尝试将您的 Uri 路径更改为...

"../Resources/a.ico"

编辑:

如果您尝试在不同的程序集中引用图像,请尝试使用 pack syntax ...

pack://application:,,,/ReferencedAssembly;component/Resources/a.ico

...其中 ReferencedAssembly 是包含您的图像的程序集。

关于wpf - 将 ImageSource 对象直接绑定(bind)到 XAML/WPF 中的图像源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4452018/

相关文章:

c# - 在多个 View 之间共享 ViewModel

c# - WPF 中 CheckBox 的自定义鼠标命中区域

wpf - 将 EF4 与 Caliburn.Micro 绑定(bind) : Should I expose my Entity as a property of the ViewModel?

java - MVP 到 MVVM Android

xaml - 根据数据绑定(bind)值设置背景颜色

c# - Microsoft.Expression.Interactions 版本 4.0.0.0 运行时版本 v4.0.30319 在哪里下载?

wpf - 保存 WPF Assets

c# - Xamarin.Forms/UWP/WPF 中的 MVVM 与使用后面的代码有何不同?

apache-flex - Flex 3 绑定(bind)问题

c# - View 更改后自动 INotifyPropertyChanged