Link an ImageSource 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 is filled in:

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 start the project, all the names of my languages ​​are displayed in the list, but not the icons ... Why is this happening and how can I make sure that my icons are displayed? (I am sure that resources exist because BitmapImages do not generate errors)

+3
source share
2 answers

Try changing your Uri route to ...

"../Resources/a.ico"

EDIT :

If you are trying to reference images in another assembly, try using the package syntax ...

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

... ReferencedAssembly - , .

+5

, , -

"/a.ico" , XAML.

, a.ico . , : ?

pack://application:,,,/MyApp.ExtAssembly;/Resources/a.ico - , ...

0

Source: https://habr.com/ru/post/1780470/


All Articles