Skip to content

Commit 87fb4eb

Browse files
Fix issue #548
1 parent ec69ebc commit 87fb4eb

File tree

1 file changed

+35
-29
lines changed

1 file changed

+35
-29
lines changed

MaterialDesignThemes.Wpf/DialogHost.cs

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,16 @@ public class DialogHost : ContentControl
6767
private Grid _contentCoverGrid;
6868
private DialogSession _session;
6969
private DialogOpenedEventHandler _attachedDialogOpenedEventHandler;
70-
private DialogClosingEventHandler _attachedDialogClosingEventHandler;
70+
private DialogClosingEventHandler _attachedDialogClosingEventHandler;
7171
private object _closeDialogExecutionParameter;
72-
private IInputElement _restoreFocusDialogClose;
72+
private IInputElement _restoreFocusDialogClose;
7373
private IInputElement _restoreFocusWindowReactivation;
7474
private Action _currentSnackbarMessageQueueUnPauseAction = null;
7575
private Action _closeCleanUp = () => { };
7676

7777
static DialogHost()
7878
{
79-
DefaultStyleKeyProperty.OverrideMetadata(typeof(DialogHost), new FrameworkPropertyMetadata(typeof(DialogHost)));
79+
DefaultStyleKeyProperty.OverrideMetadata(typeof(DialogHost), new FrameworkPropertyMetadata(typeof(DialogHost)));
8080
}
8181

8282
#region .Show overloads
@@ -158,7 +158,7 @@ public static Task<object> Show(object content, object dialogIdentifier, DialogO
158158
public static Task<object> Show(object content, object dialogIdentifier, DialogClosingEventHandler closingEventHandler)
159159
{
160160
return Show(content, dialogIdentifier, null, closingEventHandler);
161-
}
161+
}
162162

163163
/// <summary>
164164
/// Shows a modal dialog. To use, a <see cref="DialogHost"/> instance must be in a visual tree (typically this may be specified towards the root of a Window's XAML).
@@ -218,7 +218,7 @@ public DialogHost()
218218
Unloaded += OnUnloaded;
219219

220220
CommandBindings.Add(new CommandBinding(CloseDialogCommand, CloseDialogHandler, CloseDialogCanExecute));
221-
CommandBindings.Add(new CommandBinding(OpenDialogCommand, OpenDialogHandler));
221+
CommandBindings.Add(new CommandBinding(OpenDialogCommand, OpenDialogHandler));
222222
}
223223

224224
public static readonly DependencyProperty IdentifierProperty = DependencyProperty.Register(
@@ -238,7 +238,7 @@ public object Identifier
238238

239239
private static void IsOpenPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
240240
{
241-
var dialogHost = (DialogHost)dependencyObject;
241+
var dialogHost = (DialogHost)dependencyObject;
242242

243243
if (dialogHost._popupContentControl != null)
244244
ValidationAssist.SetSuppress(dialogHost._popupContentControl, !dialogHost.IsOpen);
@@ -261,8 +261,14 @@ private static void IsOpenPropertyChangedCallback(DependencyObject dependencyObj
261261
dialogHost._session.IsEnded = true;
262262
dialogHost._session = null;
263263
dialogHost._closeCleanUp();
264-
dialogHost.Dispatcher.InvokeAsync(() => dialogHost._restoreFocusDialogClose.Focus(), DispatcherPriority.Input);
265264

265+
// Don't attempt to Invoke if _restoreFocusDialogClose hasn't been assigned yet. Can occur
266+
// if the MainWindow has started up minimized. Even when Show() has been called, this doesn't
267+
// seem to have been set.
268+
if (dialogHost._restoreFocusDialogClose != null)
269+
{
270+
dialogHost.Dispatcher.InvokeAsync(() => dialogHost._restoreFocusDialogClose.Focus(), DispatcherPriority.Input);
271+
}
266272
return;
267273
}
268274

@@ -294,7 +300,7 @@ private static void IsOpenPropertyChangedCallback(DependencyObject dependencyObj
294300

295301
public bool IsOpen
296302
{
297-
get { return (bool) GetValue(IsOpenProperty); }
303+
get { return (bool)GetValue(IsOpenProperty); }
298304
set { SetValue(IsOpenProperty, value); }
299305
}
300306

@@ -303,7 +309,7 @@ public bool IsOpen
303309

304310
public object DialogContent
305311
{
306-
get { return (object) GetValue(DialogContentProperty); }
312+
get { return (object)GetValue(DialogContentProperty); }
307313
set { SetValue(DialogContentProperty, value); }
308314
}
309315

@@ -312,7 +318,7 @@ public object DialogContent
312318

313319
public DataTemplate DialogContentTemplate
314320
{
315-
get { return (DataTemplate) GetValue(DialogContentTemplateProperty); }
321+
get { return (DataTemplate)GetValue(DialogContentTemplateProperty); }
316322
set { SetValue(DialogContentTemplateProperty, value); }
317323
}
318324

@@ -321,7 +327,7 @@ public DataTemplate DialogContentTemplate
321327

322328
public DataTemplateSelector DialogContentTemplateSelector
323329
{
324-
get { return (DataTemplateSelector) GetValue(DialogContentTemplateSelectorProperty); }
330+
get { return (DataTemplateSelector)GetValue(DialogContentTemplateSelectorProperty); }
325331
set { SetValue(DialogContentTemplateSelectorProperty, value); }
326332
}
327333

@@ -330,7 +336,7 @@ public DataTemplateSelector DialogContentTemplateSelector
330336

331337
public string DialogContentStringFormat
332338
{
333-
get { return (string) GetValue(DialogContentStringFormatProperty); }
339+
get { return (string)GetValue(DialogContentStringFormatProperty); }
334340
set { SetValue(DialogContentStringFormatProperty, value); }
335341
}
336342

@@ -339,7 +345,7 @@ public string DialogContentStringFormat
339345

340346
public Thickness DialogMargin
341347
{
342-
get { return (Thickness) GetValue(DialogMarginProperty); }
348+
get { return (Thickness)GetValue(DialogMarginProperty); }
343349
set { SetValue(DialogMarginProperty, value); }
344350
}
345351

@@ -352,7 +358,7 @@ public Thickness DialogMargin
352358
/// </summary>
353359
public DialogHostOpenDialogCommandDataContextSource OpenDialogCommandDataContextSource
354360
{
355-
get { return (DialogHostOpenDialogCommandDataContextSource) GetValue(OpenDialogCommandDataContextSourceProperty); }
361+
get { return (DialogHostOpenDialogCommandDataContextSource)GetValue(OpenDialogCommandDataContextSourceProperty); }
356362
set { SetValue(OpenDialogCommandDataContextSourceProperty, value); }
357363
}
358364

@@ -364,7 +370,7 @@ public DialogHostOpenDialogCommandDataContextSource OpenDialogCommandDataContext
364370
/// </summary>
365371
public bool CloseOnClickAway
366372
{
367-
get { return (bool) GetValue(CloseOnClickAwayProperty); }
373+
get { return (bool)GetValue(CloseOnClickAwayProperty); }
368374
set { SetValue(CloseOnClickAwayProperty, value); }
369375
}
370376

@@ -376,7 +382,7 @@ public bool CloseOnClickAway
376382
/// </summary>
377383
public object CloseOnClickAwayParameter
378384
{
379-
get { return (object) GetValue(CloseOnClickAwayParameterProperty); }
385+
get { return (object)GetValue(CloseOnClickAwayParameterProperty); }
380386
set { SetValue(CloseOnClickAwayParameterProperty, value); }
381387
}
382388

@@ -395,14 +401,14 @@ private static void SnackbarMessageQueuePropertyChangedCallback(DependencyObject
395401
if (!dialogHost.IsOpen) return;
396402
var snackbarMessageQueue = dependencyPropertyChangedEventArgs.NewValue as SnackbarMessageQueue;
397403
dialogHost._currentSnackbarMessageQueueUnPauseAction = snackbarMessageQueue?.Pause();
398-
}
404+
}
399405

400406
/// <summary>
401407
/// Allows association of a snackbar, so that notifications can be paused whilst a dialog is being displayed.
402408
/// </summary>
403409
public SnackbarMessageQueue SnackbarMessageQueue
404410
{
405-
get { return (SnackbarMessageQueue) GetValue(SnackbarMessageQueueProperty); }
411+
get { return (SnackbarMessageQueue)GetValue(SnackbarMessageQueueProperty); }
406412
set { SetValue(SnackbarMessageQueueProperty, value); }
407413
}
408414

@@ -420,7 +426,7 @@ public override void OnApplyTemplate()
420426

421427
VisualStateManager.GoToState(this, SelectState(), false);
422428

423-
base.OnApplyTemplate();
429+
base.OnApplyTemplate();
424430
}
425431

426432
#region open dialog events/callbacks
@@ -493,7 +499,7 @@ public event DialogClosingEventHandler DialogClosing
493499
add { AddHandler(DialogClosingEvent, value); }
494500
remove { RemoveHandler(DialogClosingEvent, value); }
495501
}
496-
502+
497503
/// <summary>
498504
/// Attached property which can be used on the <see cref="Button"/> which instigated the <see cref="OpenDialogCommand"/> to process the closing event.
499505
/// </summary>
@@ -507,23 +513,23 @@ public static void SetDialogClosingAttached(DependencyObject element, DialogClos
507513

508514
public static DialogClosingEventHandler GetDialogClosingAttached(DependencyObject element)
509515
{
510-
return (DialogClosingEventHandler) element.GetValue(DialogClosingAttachedProperty);
511-
}
516+
return (DialogClosingEventHandler)element.GetValue(DialogClosingAttachedProperty);
517+
}
512518

513519
public static readonly DependencyProperty DialogClosingCallbackProperty = DependencyProperty.Register(
514-
nameof(DialogClosingCallback), typeof (DialogClosingEventHandler), typeof (DialogHost), new PropertyMetadata(default(DialogClosingEventHandler)));
520+
nameof(DialogClosingCallback), typeof (DialogClosingEventHandler), typeof (DialogHost), new PropertyMetadata(default(DialogClosingEventHandler)));
515521

516522
/// <summary>
517523
/// Callback fired when the <see cref="DialogClosing"/> event is fired, allowing the event to be processed from a binding/view model.
518524
/// </summary>
519525
public DialogClosingEventHandler DialogClosingCallback
520526
{
521-
get { return (DialogClosingEventHandler) GetValue(DialogClosingCallbackProperty); }
527+
get { return (DialogClosingEventHandler)GetValue(DialogClosingCallbackProperty); }
522528
set { SetValue(DialogClosingCallbackProperty, value); }
523529
}
524530

525531
protected void OnDialogClosing(DialogClosingEventArgs eventArgs)
526-
{
532+
{
527533
RaiseEvent(eventArgs);
528534
}
529535

@@ -541,7 +547,7 @@ internal void Close(object parameter)
541547
{
542548
var dialogClosingEventArgs = new DialogClosingEventArgs(_session, parameter, DialogClosingEvent);
543549

544-
_session.IsEnded = true;
550+
_session.IsEnded = true;
545551

546552
//multiple ways of calling back that the dialog is closing:
547553
// * routed event
@@ -556,7 +562,7 @@ internal void Close(object parameter)
556562
if (!dialogClosingEventArgs.IsCancelled)
557563
SetCurrentValue(IsOpenProperty, false);
558564
else
559-
_session.IsEnded = false;
565+
_session.IsEnded = false;
560566

561567
_closeDialogExecutionParameter = parameter;
562568
}
@@ -681,7 +687,7 @@ private static void WatchWindowActivation(DialogHost dialogHost)
681687
}
682688

683689
private void WindowOnDeactivated(object sender, EventArgs eventArgs)
684-
{
690+
{
685691
_restoreFocusWindowReactivation = _popup != null ? FocusManager.GetFocusedElement((Window)sender) : null;
686692
}
687693

@@ -690,7 +696,7 @@ private void WindowOnActivated(object sender, EventArgs eventArgs)
690696
if (_restoreFocusWindowReactivation != null)
691697
{
692698
Dispatcher.BeginInvoke(new Action(() =>
693-
{
699+
{
694700
Keyboard.Focus(_restoreFocusWindowReactivation);
695701
}));
696702
}

0 commit comments

Comments
 (0)