Skip to content

Commit 5df62fa

Browse files
corvinszCorvin SzimionKeboo
authored
Feature - Blurry Dialog Background (#3738)
* Cleanup of launchSettings and Cards Page in Demo * Revert "Cleanup of launchSettings and Cards Page in Demo" This reverts commit ede38d4. * -added DPs to DialogHost ("ApplyBlurBackground" and "BlurRadius") -added DialogBackgroundBlurConverter -Blurry Background working for both the "normal" DialogHost Style and the "MaterialDesignEmbeddedDialogHost" * Added static "Instance" member to "DialogBackgroundBlurConverter" * code cleanup in DialogHost.cs and DialogHost-Style * cleanup demo app * converter nullability * Update src/MaterialDesignThemes.Wpf/Converters/DialogBackgroundBlurConverter.cs converter nullability Co-authored-by: Kevin B <[email protected]> --------- Co-authored-by: Corvin Szimion <[email protected]> Co-authored-by: Kevin B <[email protected]>
1 parent ff4418f commit 5df62fa

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Windows.Data;
8+
using System.Windows.Media.Effects;
9+
10+
namespace MaterialDesignThemes.Wpf.Converters;
11+
internal sealed class DialogBackgroundBlurConverter : IMultiValueConverter
12+
{
13+
public static readonly DialogBackgroundBlurConverter Instance = new();
14+
public object? Convert(object?[]? values, Type targetType, object? parameter, CultureInfo culture)
15+
{
16+
if (values is [bool isOpen, bool applyBlurBackground, double blurRadius]
17+
&& isOpen
18+
&& applyBlurBackground)
19+
{
20+
return new BlurEffect() { Radius = blurRadius };
21+
}
22+
23+
return null;
24+
}
25+
public object?[]? ConvertBack(object? value, Type[] targetTypes, object? parameter, CultureInfo culture) => throw new NotImplementedException();
26+
}

src/MaterialDesignThemes.Wpf/DialogHost.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,24 @@ public Brush? DialogBackground
598598
set => SetValue(DialogBackgroundProperty, value);
599599
}
600600

601+
public bool ApplyBlurBackground
602+
{
603+
get => (bool)GetValue(ApplyBlurBackgroundProperty);
604+
set => SetValue(ApplyBlurBackgroundProperty, value);
605+
}
606+
public static readonly DependencyProperty ApplyBlurBackgroundProperty = DependencyProperty.Register(
607+
nameof(ApplyBlurBackground), typeof(bool), typeof(DialogHost), new PropertyMetadata(default(bool)));
608+
609+
610+
private const double DefaultBlurRadius = 16.0;
611+
public double BlurRadius
612+
{
613+
get => (double)GetValue(BlurRadiusProperty);
614+
set => SetValue(BlurRadiusProperty, value);
615+
}
616+
public static readonly DependencyProperty BlurRadiusProperty = DependencyProperty.Register(
617+
nameof(BlurRadius), typeof(double), typeof(DialogHost), new PropertyMetadata(DefaultBlurRadius));
618+
601619
public override void OnApplyTemplate()
602620
{
603621
if (_contentCoverGrid != null)
@@ -624,14 +642,14 @@ public static void SetRestoreFocusElement(DependencyObject element, IInputElemen
624642
=> element.SetValue(RestoreFocusElementProperty, value);
625643

626644
public static IInputElement GetRestoreFocusElement(DependencyObject element)
627-
=> (IInputElement) element.GetValue(RestoreFocusElementProperty);
645+
=> (IInputElement)element.GetValue(RestoreFocusElementProperty);
628646

629647
public static readonly DependencyProperty IsRestoreFocusDisabledProperty = DependencyProperty.Register(
630648
nameof(IsRestoreFocusDisabled), typeof(bool), typeof(DialogHost), new PropertyMetadata(false));
631649

632650
public bool IsRestoreFocusDisabled
633651
{
634-
get => (bool) GetValue(IsRestoreFocusDisabledProperty);
652+
get => (bool)GetValue(IsRestoreFocusDisabledProperty);
635653
set => SetValue(IsRestoreFocusDisabledProperty, value);
636654
}
637655

@@ -958,7 +976,7 @@ private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
958976

959977
private void OnPreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
960978
{
961-
979+
962980
}
963981

964982
[SecurityCritical]

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,15 @@
198198
Content="{TemplateBinding ContentControl.Content}"
199199
ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"
200200
ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
201-
Opacity="1" />
201+
Opacity="1">
202+
<ContentPresenter.Effect>
203+
<MultiBinding Converter="{x:Static converters:DialogBackgroundBlurConverter.Instance}">
204+
<Binding Path="IsOpen" RelativeSource="{RelativeSource TemplatedParent}" />
205+
<Binding Path="ApplyBlurBackground" RelativeSource="{RelativeSource TemplatedParent}" />
206+
<Binding Path="BlurRadius" RelativeSource="{RelativeSource TemplatedParent}" />
207+
</MultiBinding>
208+
</ContentPresenter.Effect>
209+
</ContentPresenter>
202210
</AdornerDecorator>
203211
<Grid x:Name="PART_ContentCoverGrid"
204212
Background="Transparent"
@@ -380,7 +388,15 @@
380388
Content="{TemplateBinding ContentControl.Content}"
381389
ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"
382390
ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
383-
Opacity="1" />
391+
Opacity="1">
392+
<ContentPresenter.Effect>
393+
<MultiBinding Converter="{x:Static converters:DialogBackgroundBlurConverter.Instance}">
394+
<Binding Path="IsOpen" RelativeSource="{RelativeSource TemplatedParent}" />
395+
<Binding Path="ApplyBlurBackground" RelativeSource="{RelativeSource TemplatedParent}" />
396+
<Binding Path="BlurRadius" RelativeSource="{RelativeSource TemplatedParent}" />
397+
</MultiBinding>
398+
</ContentPresenter.Effect>
399+
</ContentPresenter>
384400

385401
<Grid x:Name="PART_ContentCoverGrid"
386402
Background="Transparent"

0 commit comments

Comments
 (0)