|
| 1 | +[Home](..\README.md) > How to increase rendering performance? |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +# How to increase rendering performance? |
| 6 | + |
| 7 | +## Background information |
| 8 | + |
| 9 | +Every class inheriting from [`UIElement`](https://learn.microsoft.com/dotnet/api/system.windows.uielement?view=windowsdesktop-8.0) |
| 10 | +contains a property [`CacheMode`](https://learn.microsoft.com/dotnet/api/system.windows.uielement.cachemode?view=windowsdesktop-8.0). To quote Microsoft's documentation: |
| 11 | + |
| 12 | +> Set the CacheMode property when you need to increase performance for content that is time consuming to render. For |
| 13 | +> more information, see [BitmapCache](https://learn.microsoft.com/dotnet/api/system.windows.media.bitmapcache?view=windowsdesktop-8.0). |
| 14 | +
|
| 15 | +The default value is `null` as to not use any form of caching. This makes the controls sharp and crisp. |
| 16 | + |
| 17 | +## Setting `UIElement.CacheMode` |
| 18 | + |
| 19 | +An example how to set a `CacheMode`: |
| 20 | + |
| 21 | +```xaml |
| 22 | +<!-- This should decrease rendering time --> |
| 23 | + |
| 24 | +<ToggleButton> |
| 25 | + <ToggleButton.CacheMode> |
| 26 | + <BitmapCache |
| 27 | + EnableClearType="True" |
| 28 | + RenderAtScale="1" |
| 29 | + SnapsToDevicePixels="True" /> |
| 30 | + </ToggleButton.CacheMode> |
| 31 | +</ToggleButton> |
| 32 | +``` |
| 33 | + |
| 34 | +Increase the `RenderAtScale` value, will sharpen the control, but it will also make it more pixelized when drawn smaller. |
| 35 | + |
| 36 | +> [!NOTE] |
| 37 | +> The default value of `UIElement.CacheMode` is `null`. |
| 38 | +
|
| 39 | +## Advanced: setting `ShadowAssist.CacheMode` |
| 40 | + |
| 41 | +Material Design in XAML toolkit also provides you with an attached property `ShadowAssist.CacheMode`. |
| 42 | +This attached property is used in places where a simple `CacheMode` property would not suffice. This could be in situations |
| 43 | +where the property should be inherited, as `UIElement.CacheMode` does not support property inheritance. |
| 44 | + |
| 45 | +This attached property is set through binding on a `CacheMode` property under the parent control. |
| 46 | + |
| 47 | +An example of this property being used: |
| 48 | +```xaml |
| 49 | +<!-- Found inside MaterialDesignTheme.ToggleButton.xaml --> |
| 50 | + |
| 51 | +<AdornerDecorator CacheMode="{Binding RelativeSource={RelativeSource Self}, Path=(wpf:ShadowAssist.CacheMode)}"> |
| 52 | + <Ellipse x:Name="Thumb" ... /> |
| 53 | +</AdornerDecorator> |
| 54 | +``` |
| 55 | + |
| 56 | +> [!NOTE] |
| 57 | +> The default value of `ShadowAssist.CacheMode` is `null`. |
| 58 | +
|
| 59 | +## Example |
| 60 | + |
| 61 | +| With `CacheMode` set | Without `CacheMode` set | |
| 62 | +| --------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- | |
| 63 | +|  |  | |
| 64 | + |
| 65 | +## Further reading |
| 66 | + |
| 67 | +Some interesting articles with more in-depth information: |
| 68 | +* [Property value inheritance (WPF .NET)](https://learn.microsoft.com/dotnet/desktop/wpf/properties/property-value-inheritance?view=netdesktop-7.0) |
| 69 | +* [UIElement.CacheMode Property](https://learn.microsoft.com/dotnet/api/system.windows.uielement.cachemode?view=windowsdesktop-8.0) |
0 commit comments