开发者

Binding an ImageSource object directly to an Image source in XAML/WPF?

I have the following XAML code:

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

and in the model class:

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

my modelview class contains:

public List<Language> Languages;

which gets filled with:

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))));  

When I run the project, all my language names are shown in the list, but not the icons... Why does this happen and how can I ensure 开发者_如何转开发that my icons are shown? (I am sure that the resources do exist as the BitmapImages don't throw errors)


Try changing your Uri path to...

"../Resources/a.ico"

EDIT:

If you are trying to reference the images in a differing assembly try using the pack syntax...

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

...where ReferencedAssembly is the assembly containing your images.


Well I got the error reason but still no solution -

The "Resources/a.ico" simply wasn't found - so it's no XAML problem anymore.

But it's that circumstance that a.ico is located in an other Assembly. So now the question is: How to get to this icon?

Now I used the pack://application:,,,/MyApp.ExtAssembly;/Resources/a.ico - but it always throw an exception that it wasn't found...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜