Skip to content

Commit 5e856ef

Browse files
MichelMichelsKeboo
andauthored
Fix #3612 - Default value of ShadowAssist.CacheMode set to 'null' (#3616)
* Fix #3612 - Default value of ShadowAssist.CacheMode set to 'null' * CacheMode changed to ShadowAssist.CacheMode binding * Add documentation about CacheMode and ShadowAssist.CacheMode * Added source articles * Update docs/rendering-performance.md * Update docs/rendering-performance.md * Update docs/rendering-performance.md * Update docs/rendering-performance.md * Update docs/rendering-performance.md --------- Co-authored-by: Kevin B <[email protected]>
1 parent cd05f57 commit 5e856ef

File tree

4 files changed

+77
-6
lines changed

4 files changed

+77
-6
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Comprehensive and easy to use Material Design theme and control library for the
3232
- [Building the source](#building-the-source)
3333
- [Screenshots](#screenshots)
3434
- [More examples](#more-examples)
35+
- [FAQ](#faq)
3536
- [Contributing](#contributing)
3637
- [Mentions](#mentions)
3738
- [Backers](#backers)
@@ -134,6 +135,10 @@ This repository also contains 3 different demo applications:
134135
* [F1ix](http://materialdesigninxaml.net/f1ix)
135136
* [Motion List](https://github.com/MaterialDesignInXAML/MotionList)
136137

138+
## FAQ
139+
140+
* [How to increase rendering performance?](docs/rendering-performance.md)
141+
137142
## Contributing
138143

139144
Before contributing code read the [Contribution Guidelines](.github/CONTRIBUTING.md)

docs/rendering-performance.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
| ![image](https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/assets/6505319/9401be9c-9939-4c02-b37e-610707ea9e5c) | ![image](https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/assets/6505319/928e6f70-60a2-4e0a-b8e5-f1955d3cc6f4) |
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)

src/MaterialDesignThemes.Wpf/ShadowAssist.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static bool GetDarken(DependencyObject element)
7979

8080
#region AttachedProperty : CacheModeProperty
8181
public static readonly DependencyProperty CacheModeProperty = DependencyProperty.RegisterAttached(
82-
"CacheMode", typeof(CacheMode), typeof(ShadowAssist), new FrameworkPropertyMetadata(new BitmapCache { EnableClearType = true, SnapsToDevicePixels = true }, FrameworkPropertyMetadataOptions.Inherits));
82+
"CacheMode", typeof(CacheMode), typeof(ShadowAssist), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));
8383

8484
public static void SetCacheMode(DependencyObject element, CacheMode value)
8585
{

src/MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.ToolBar.xaml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,8 @@
186186
IsOpen="{Binding IsOverflowOpen, RelativeSource={RelativeSource TemplatedParent}}"
187187
Placement="Bottom"
188188
PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}"
189-
StaysOpen="false">
190-
<!-- warning, this will cache the inner item as well, consider separating the shadow from the item if possible -->
191-
<Popup.CacheMode>
192-
<BitmapCache EnableClearType="True" SnapsToDevicePixels="True" />
193-
</Popup.CacheMode>
189+
StaysOpen="false"
190+
CacheMode="{Binding RelativeSource={RelativeSource Self}, Path=(wpf:ShadowAssist.CacheMode)}">
194191
<Border x:Name="ToolBarSubMenuBorder"
195192
Margin="1"
196193
Background="{DynamicResource MaterialDesign.Brush.ToolBar.Background}"

0 commit comments

Comments
 (0)