Using WPF, how can we use BitmapImages that are defined in a ResourceDictionary with IconSources and IconResources

1 Answer 96 Views
Styling
Peter
Top achievements
Rank 1
Peter asked on 18 Sep 2022, 03:46 PM

In our applications we have several large ResourceDictionaries that contain many images that we then use throughout our applications.

For example, we have a "refresh" icon defined as :

<BitmapImage x:Key="Refresh32Image" UriSource="Refresh32px.png"/>

Then whenever we want to show that image on any WPF window we just put this:

 <Button x:Name="RefreshButton">
        <Image Source="{StaticResource Refresh32Image}"/>
</Button>

We have this in dozens of different windows etc.

We are now switching over to use Telerik Implicit Theming and we can make this work by adding an IconSource at the top of each window and changing the buttons content to :

<Image Style="{StaticResource Image16Style}"
       Source="{telerik:IconResource IconRelativePath=Refresh32px.png,IconSources={StaticResource CommonIconSources}}"/>
 

This does work, however, requires a LOT of code changes throughout the application. We have hundreds of buttons that would all need updating with various different image filenames etc. etc.

So ideally we want to change the Bitmap Image Resource instead, so that we don't have to change the button content, and of course, any new buttons that we want to add are much simpler.

However, we cannot find  way to do this.  We have tried this, so instead of declaring the Refresh32Image as a BitmapResource, instead it's an IconResource

<telerik:IconResource x:Key="Refresh32Image" IconRelativePath="Refresh32px.png" IconSources="{StaticResource CommonIconSources}" />

However, this won't compile because when we then try to use that resource in a button, WPF shows:

'Telerik.Windows.Controls.IconResourceExtension' is not a valid value for property

We also tried declaring the image resource as 


<BitmapImage x:Key="Refresh32Image" UriSource="{telerik:IconResource IconRelativePath=Refresh32px.png,IconSources={StaticResource CommonIconSources}}"/>

But this is rejected as 

Property 'UriSource' or property 'StreamSource' must be set.

 

I'm sure there must be an easy way of declaring BitmapImage resources just once and reusing them wherever we need using IconResources, so can you help us work out how to do this?

1 Answer, 1 is accepted

Sort by
0
Masha
Telerik team
answered on 21 Sep 2022, 05:46 PM

Hello Peter,

Unfortunately, there is no way to achieve the desired approach  except by setting the Image source as :

<Image Style="{StaticResource Image16Style}"
       Source="{telerik:IconResource IconRelativePath=Refresh32px.png,IconSources={StaticResource CommonIconSources}}"/>

More about IconSources you can read in our online documentation.

I'm sorry for the inconvenience caused.

Regards,
Masha
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Styling
Asked by
Peter
Top achievements
Rank 1
Answers by
Masha
Telerik team
Share this question
or