Skip to content

Commit 5ff9626

Browse files
committed
added BlurBackground to the DialogOptions and showcase it the demo
1 parent e43653e commit 5ff9626

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

src/MainDemo.Wpf/Dialogs.xaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@
5454
<CheckBox x:Name="cbCloseOnClickAway"
5555
Content="CloseOnClickAway"
5656
IsChecked="False" />
57+
<CheckBox x:Name="cbApplyBlurEffect"
58+
Content="ApplyBlurEffect"
59+
IsChecked="False" />
60+
<TextBox x:Name="tbBlurRadius"
61+
materialDesign:HintAssist.Hint="BlurRadius"
62+
Style="{StaticResource MaterialDesignFilledTextBox}" />
5763
</StackPanel>
5864
</GroupBox>
5965
<Button Margin="0,8,0,0"

src/MainDemo.Wpf/Dialogs.xaml.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public Dialogs()
1212
DataContext = new DialogsViewModel();
1313
InitializeComponent();
1414
BlurRadiusSlider.Value = DialogHost.DefaultBlurRadius;
15+
tbBlurRadius.Text = DialogOptions.Default.BlurRadius.ToString();
1516
}
1617

1718
private void Sample1_DialogHost_OnDialogClosing(object sender, DialogClosingEventArgs eventArgs)
@@ -99,13 +100,15 @@ private async void Sample7_OpenWithDialogOptions(object sender, RoutedEventArgs
99100
IsFullscreen = cbIsFullscreen.IsChecked!.Value,
100101
ShowCloseButton = cbShowCloseButton.IsChecked!.Value,
101102
CloseOnClickAway = cbCloseOnClickAway.IsChecked!.Value,
103+
ApplyBlurEffect = cbApplyBlurEffect.IsChecked!.Value,
104+
BlurRadius = double.TryParse(tbBlurRadius.Text, out double parsedRadius) ? parsedRadius : 0
102105
};
103106

104107
var dialogContent = new TextBlock()
105108
{
106109
Text = "Some dialog content",
107110
Margin = new Thickness(32)
108111
};
109-
await DialogHost.Show<int>(dialogContent, "RootDialog", options);
112+
await DialogHost.Show(dialogContent, "RootDialog", options);
110113
}
111114
}

src/MaterialDesignThemes.Wpf/DialogHost.cs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Windows.Data;
44
using System.Windows.Interop;
55
using System.Windows.Media;
6+
using System.Windows.Media.Effects;
67
using System.Windows.Threading;
78

89
namespace MaterialDesignThemes.Wpf;
@@ -15,11 +16,12 @@ public record DialogOptions
1516
internal double PreviousDialogContentUniformCornerRadius { get; set; }
1617
internal double PreviousPopupHeight { get; set; }
1718
internal double PreviousPopupWidth { get; set; }
18-
19+
internal bool PreviousApplyBlurEffect { get; set; }
20+
internal double PreviousBlurRadius { get; set; }
1921

2022
public static readonly DialogOptions Default = new();
2123

22-
// maybe in the future we should have a "OpeningEventHandler" if we already have a "ClosingEventHandler"?
24+
// if there is demand for it we could have a "OpeningEventHandler" too
2325
public DialogOpenedEventHandler? OpenedEventHandler { get; set; } = null;
2426
public DialogClosingEventHandler? ClosingEventHandler { get; set; } = null;
2527
public DialogClosedEventHandler? ClosedEventHandler { get; set; } = null;
@@ -29,8 +31,8 @@ public record DialogOptions
2931

3032
public bool CloseOnClickAway { get; set; } = false;
3133

32-
//public bool ApplyBlurEffect { get; set; } = false;
33-
//public double BlurRadius { get; set; } = 16d;
34+
public bool ApplyBlurEffect { get; set; } = false;
35+
public double BlurRadius { get; set; } = 16d;
3436
}
3537

3638
/// <summary>
@@ -68,6 +70,7 @@ public class DialogHost : ContentControl
6870
public const string CloseButtonPartName = "PART_CloseButton";
6971
public const string OpenStateName = "Open";
7072
public const string ClosedStateName = "Closed";
73+
public const string ContentPresenterName = "ContentPresenter";
7174

7275
/// <summary>
7376
/// Routed command to be used somewhere inside an instance to trigger showing of the dialog. Content can be passed to the dialog via a <see cref="Button.CommandParameter"/>.
@@ -89,6 +92,7 @@ public class DialogHost : ContentControl
8992
private ContentControl? _popupContentControl;
9093
private Grid? _contentCoverGrid;
9194
private Button? _closeButton;
95+
private ContentPresenter? _contentPresenter;
9296
private DialogOpenedEventHandler? _attachedDialogOpenedEventHandler;
9397
private DialogClosingEventHandler? _attachedDialogClosingEventHandler;
9498
private DialogClosedEventHandler? _attachedDialogClosedEventHandler;
@@ -371,6 +375,17 @@ private void ApplyDialogOptions(DialogOptions options)
371375

372376
SetPopupSize(ActualHeight, ActualWidth);
373377
}
378+
379+
if (options.ApplyBlurEffect && _contentPresenter is not null)
380+
{
381+
options.PreviousApplyBlurEffect = ApplyBlurBackground;
382+
options.BlurRadius = BlurRadius;
383+
384+
_contentPresenter.Effect = new BlurEffect()
385+
{
386+
Radius = options.BlurRadius
387+
};
388+
}
374389
}
375390

376391
private void RevertDialogOptions(DialogOptions options)
@@ -399,6 +414,13 @@ private void RevertDialogOptions(DialogOptions options)
399414

400415
SetPopupSize(options.PreviousPopupHeight, options.PreviousPopupWidth);
401416
}
417+
418+
if (options.ApplyBlurEffect && _contentPresenter is not null)
419+
{
420+
_contentPresenter.Effect = null;
421+
ApplyBlurBackground = options.PreviousApplyBlurEffect;
422+
BlurRadius = options.BlurRadius;
423+
}
402424
}
403425

404426
internal async Task<object?> ShowInternal(object content, DialogOptions options)
@@ -796,6 +818,7 @@ public override void OnApplyTemplate()
796818
_popupContentControl = GetTemplateChild(PopupContentPartName) as ContentControl;
797819
_contentCoverGrid = GetTemplateChild(ContentCoverGridName) as Grid;
798820
_closeButton = GetTemplateChild(CloseButtonPartName) as Button;
821+
_contentPresenter = GetTemplateChild(ContentPresenterName) as ContentPresenter;
799822

800823
if (_contentCoverGrid is not null)
801824
_contentCoverGrid.MouseLeftButtonUp += ContentCoverGridOnMouseLeftButtonUp;

0 commit comments

Comments
 (0)