# Switching Icons at Runtime

By utilizing the [implicit styling approach ](https://www.telerik.com/products/wpf/documentation/styling-and-appearance/styling-apperance-setting-a-theme-overview) you have the ability to switch the theme of Telerik WPF controls at run-time. With the large set of themes available in the suite this can drastically change the look and feel of your application.

To help you achieve a consistent look we also provide a way for you to dynamically change the images in your application through the **IconResource** [markup extension](https://msdn.microsoft.com/en-us/library/ms747254\(v=vs.100\).aspx). The extension allows you to define three different URI-based paths which point to set of icons and then easily swap one set with another.

## How To Use IconSources

To take advantage of the extension you need to define a resource of type **IconSources** holding the paths to the sets of icons. **Example 1** demonstrates how to define a resource in XAML.

**Example 1: Creating IconSources**

```XAML
    <telerik:IconSources x:Key="IconSources" />
```

> **IconSources** resides in the **Telerik.Windows.Controls.dll**, so you need to add a reference to the assembly  to your project first.

**IconSources** has three distinct properties to which you can assign base paths for your images.

* **LightBasePath**
* **DarkBasePath**
* **ModernBasePath**

The **LightBasePath** property represents a path to the default icon set destination, so setting it is mandatory.

**Example 2** shows an example on how to define **IconSources** and set two of its path properties.

**Example 2: Setting Paths to IconSources**

```XAML
    <telerik:IconSources x:Key="IconPaths" LightBasePath="/IconSourceDemo;component/Icons/Light/" DarkBasePath="/IconSourceDemo;component/Icons/Dark/" />
```

Once the paths are set, the **IconResource** extension can be used to set the source of objects of type **ImageSource**. **Example 3** shows how to define an **Image** object in XAML pointing to the *background.png* image in the folder paths from **Example 2**.

**Example 3: Setting ImageSource Using IconResource**

```XAML
    <Image Source="{telerik:IconResource IconRelativePath=background.png, IconSources={StaticResource IconPaths}}" Stretch="None" />
```

Presuming there is an image with file name *background.png* in the *Icons/Light/* folder of your application, **Figure 1** shows the result of **Examples 1 – 3**.

> caption ImageSource set by IconResource

![Telerik UI for WPF icon resource assigned to an ImageSource](https://www.telerik.com/products/wpf/documentation/assets/d919dc460b4e122b4d92490abeb063a0/switching_icons_runtime_01.png)

Changing the image is achieved by calling the static **ChangeIconsSet()** method of **IconSources**.

<a name="example4"><a/>

**Example 4: Changing Icon Set**

```C#
	IconSources.ChangeIconsSet(IconsSet.Dark);
```

This will trigger a change in the source of the **Image** and will show the *background.png* image located in the *Icons/Dark* folder of your application.

> importantBecause the relative path to the icon is defined for each image source, for the switch to work the images in the separate folders need to be with the same names.

**Figure 2** shows the result after the code in **Example 4** is executed.

> caption Image after changing the IconSet

![Telerik UI for WPF control after changing its icon set](https://www.telerik.com/products/wpf/documentation/assets/25a652e874f0a1ee45f8ad794fd9cfe1/switching_icons_runtime_02.png)

> tip You can find the complete code of the previous example in our online [Telerik XAML SDK Common samples repository](https://github.com/telerik/xaml-sdk/tree/master/Common).

## Available Icon Sets

Several of the controls in the Telerik UI for WPF suite come with sets of icons that can be used with them. The paths to all such images are consistent with the requirements imposed by the **IconResource** extension, so you can use it to switch those icons run-time.

**RadPdfViewer**, for example, comes with two distinct sets of icons located in the **Telerik.Windows.Controls.FixedDocumentViewers** assembly. Defining an **IconSources** resource for the viewer is shown on **Example 5**.

**Example 5: Creating IconSources for RadPdfViewer**

```XAML
    <telerik:IconSources x:Key="PdfViewerIconPaths" LightBasePath="/Telerik.Windows.Controls.FixedDocumentViewers;component/Images/Modern/" 
                             DarkBasePath="/Telerik.Windows.Controls.FixedDocumentViewers;component/Images/" />
```

And you can use this resource to define images pointing to one of the base paths location.

**Example 6: ImageSource Set to RadPdfViewer Icon**

```XAML
    <Image Source="{telerik:IconResource IconRelativePath=open.png,IconSources={StaticResource PdfViewerIconPaths}}" Stretch="None" />
```

Both **IconSource** resources defined in the article are in the same view pointing to different base paths for images. The result of **Example 6** is shown on **Figure 3**.

> caption ImageSource set to RadPdfViewer icon

![Telerik UI for WPF RadPdfViewer icon assigned to an ImageSource](https://www.telerik.com/products/wpf/documentation/assets/f9775e3d0ff64c9d60595640d4056946/switching_icons_runtime_03.png)

Changing the icon set used in the application with the code from **Example 4** will change it for both images, regardless of the fact that they use separate base paths. The result is demonstrated on **Figure 4**.

> caption Images after changing the IconSet

![Telerik UI for WPF controls after changing their icon set](https://www.telerik.com/products/wpf/documentation/assets/299a723047cbfd5151b4d0eacc675ea4/switching_icons_runtime_04.png)

> tip You can find the complete code of the previous example in our online [Telerik XAML SDK Common samples repository](https://github.com/telerik/xaml-sdk/tree/master/Common).

## See Also

* [Set a Theme with Implicit Styles](https://www.telerik.com/products/wpf/documentation/styling-and-appearance/styling-apperance-setting-a-theme-overview)
* [Switch RadPdfViewer Icons at Runtime](https://www.telerik.com/products/wpf/documentation/controls/radpdfviewer/ui/switching-icons-at-runtime)
* [Switch RadSpreadsheet Icons at Runtime](https://www.telerik.com/products/wpf/documentation/controls/radspreadsheet/spreadsheet-ui#switching-icons-at-runtime)
* [Switch RadRichTextBox Icons at Runtime](https://www.telerik.com/products/wpf/documentation/controls/radrichtextbox/how-to/switching-icons-at-runtime)
