shahid shaikh
shahid shaikh

Reputation: 3

WPF icon repository

I have list of icon which will be displayed for different button i.e. save delete. what I want to do is to list down all the icons in some xaml file like app.xaml

<Resource x:Key="error"  Source="Icons/Error.ico" />              
<Resource x:Key="save"  Source="Icons/save.ico" />

then want to access same thing in individual file as follow.

Icon="{Binding save}" 

I would appreciate if someone suggest me correct approach if this is not correct.

Upvotes: 0

Views: 873

Answers (1)

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50752

  1. Create resource dictionary Images.xaml
  2. Add all images to that dictionary in this form

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <BitmapImage x:Key="Icon1"
                     UriSource="Images/icon1.png" />
       .....
    </ResourceDictionary>
    
  3. When you want to use it,

    <Image Source={StaticResource Icon1} />
    

Dont forgot to include that Image.xaml, to the place where you want to use it... actually you can merge it directly to your main dictionary in App.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <ResourceDictionary.MergedDictionaries>
       <ResourceDictionary Source="Themes/Images.xaml"/>
   </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

Upvotes: 1

Related Questions