Skip to content

Commit b97157a

Browse files
committed
dialog sessions, and opening event. thoroughly untested right now :)
1 parent 44f02d9 commit b97157a

File tree

7 files changed

+327
-44
lines changed

7 files changed

+327
-44
lines changed

MainDemo.Wpf/Domain/DialogsViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ private async void ExecuteRunDialog(object o)
2020
{
2121
DataContext = new object()
2222
};
23-
23+
2424
//show the dialog
2525
var result = await DialogHost.Show(view, "RootDialog", ClosingEventHandler);
2626

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,61 @@
1-
using System.Windows;
1+
using System;
2+
using System.Windows;
23

34
namespace MaterialDesignThemes.Wpf
45
{
56
public class DialogClosingEventArgs : RoutedEventArgs
67
{
7-
public DialogClosingEventArgs(object parameter, object content)
8+
public DialogClosingEventArgs(DialogSession session, object parameter)
89
{
9-
Parameter = parameter;
10-
Content = content;
10+
if (session == null) throw new ArgumentNullException(nameof(session));
11+
Session = session;
12+
13+
Parameter = parameter;
1114
}
1215

13-
public DialogClosingEventArgs(object parameter, object content, RoutedEvent routedEvent) : base(routedEvent)
16+
public DialogClosingEventArgs(DialogSession session, object parameter, RoutedEvent routedEvent) : base(routedEvent)
1417
{
15-
Parameter = parameter;
16-
Content = content;
18+
if (session == null) throw new ArgumentNullException(nameof(session));
19+
Session = session;
20+
21+
Parameter = parameter;
1722
}
1823

19-
public DialogClosingEventArgs(object parameter, object content, RoutedEvent routedEvent, object source) : base(routedEvent, source)
24+
public DialogClosingEventArgs(DialogSession session, object parameter, RoutedEvent routedEvent, object source) : base(routedEvent, source)
2025
{
21-
Parameter = parameter;
22-
Content = content;
26+
if (session == null) throw new ArgumentNullException(nameof(session));
27+
Session = session;
28+
29+
Parameter = parameter;
2330
}
2431

32+
/// <summary>
33+
/// Cancel the close.
34+
/// </summary>
2535
public void Cancel()
2636
{
2737
IsCancelled = true;
2838
}
2939

40+
/// <summary>
41+
/// Indicates if the close has already been cancelled.
42+
/// </summary>
3043
public bool IsCancelled { get; private set; }
3144

3245
/// <summary>
3346
/// Gets the paramter originally provided to <see cref="DialogHost.CloseDialogCommand"/>/
3447
/// </summary>
3548
public object Parameter { get; }
3649

50+
/// <summary>
51+
/// Allows interation with the current dialog session.
52+
/// </summary>
53+
public DialogSession Session { get; }
54+
3755
/// <summary>
3856
/// Gets the <see cref="DialogHost.DialogContent"/> which is currently displayed, so this could be a view model or a UI element.
3957
/// </summary>
40-
public object Content { get; }
58+
[Obsolete("Prefer Session.Content")]
59+
public object Content => Session.Content;
4160
}
4261
}

MaterialDesignThemes.Wpf/DialogHost.cs

Lines changed: 199 additions & 32 deletions
Large diffs are not rendered by default.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Windows;
3+
4+
namespace MaterialDesignThemes.Wpf
5+
{
6+
public class DialogOpenedEventArgs : RoutedEventArgs
7+
{
8+
public DialogOpenedEventArgs(DialogSession session, RoutedEvent routedEvent)
9+
{
10+
if (session == null) throw new ArgumentNullException(nameof(session));
11+
12+
Session = session;
13+
RoutedEvent = routedEvent;
14+
}
15+
16+
/// <summary>
17+
/// Allows interation with the current dialog session.
18+
/// </summary>
19+
public DialogSession Session { get; }
20+
}
21+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
namespace MaterialDesignThemes.Wpf
2+
{
3+
public delegate void DialogOpenedEventHandler(object sender, DialogOpenedEventArgs eventArgs);
4+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
3+
namespace MaterialDesignThemes.Wpf
4+
{
5+
/// <summary>
6+
/// Allows an open dialog to be managed. Use is only permitted during a single display operation.
7+
/// </summary>
8+
public class DialogSession
9+
{
10+
private readonly DialogHost _owner;
11+
internal bool IsDisabled;
12+
13+
internal DialogSession(DialogHost owner)
14+
{
15+
if (owner == null) throw new ArgumentNullException(nameof(owner));
16+
17+
_owner = owner;
18+
}
19+
20+
/// <summary>
21+
/// Gets the <see cref="DialogHost.DialogContent"/> which is currently displayed, so this could be a view model or a UI element.
22+
/// </summary>
23+
/// <exception cref="InvalidOperationException">Thrown if the dialog session has ended.</exception>
24+
public object Content
25+
{
26+
get
27+
{
28+
if (IsDisabled) throw new InvalidOperationException("Dialog session has ended.");
29+
30+
return _owner.DialogContent;
31+
}
32+
}
33+
34+
/// <summary>
35+
/// Update the currrent content in the dialog.
36+
/// </summary>
37+
/// <param name="content"></param>
38+
/// <exception cref="InvalidOperationException">Thrown if the dialog content is currently set via a binding, or if the dialog session has ended.</exception>
39+
public void UpdateContent(object content)
40+
{
41+
if (content == null) throw new ArgumentNullException(nameof(content));
42+
if (IsDisabled) throw new InvalidOperationException("Dialog session has ended.");
43+
44+
_owner.AssertTargetableContent();
45+
_owner.DialogContent = content;
46+
}
47+
48+
/// <summary>
49+
/// Closes the dialog.
50+
/// </summary>
51+
/// <exception cref="InvalidOperationException">Thrown if the dialog session has ended, or a close operation is currently in progress.</exception>
52+
public void Close()
53+
{
54+
if (IsDisabled) throw new InvalidOperationException("Dialog session has ended.");
55+
56+
}
57+
58+
/// <summary>
59+
/// Closes the dialog.
60+
/// </summary>
61+
/// <param name="parameter">Result parameter which will be returned in <see cref="DialogClosingEventArgs.Parameter"/> or from <see cref="DialogHost.Show(object)"/> method.</param>
62+
/// <exception cref="InvalidOperationException">Thrown if the dialog session has ended, or a close operation is currently in progress.</exception>
63+
public void Close(object parameter)
64+
{
65+
if (IsDisabled) throw new InvalidOperationException("Dialog session has ended.");
66+
67+
}
68+
}
69+
}

MaterialDesignThemes.Wpf/MaterialDesignThemes.Wpf.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@
229229
<Compile Include="DateTimeEx.cs" />
230230
<Compile Include="DialogClosingEventArgs.cs" />
231231
<Compile Include="DialogClosingEventHandler.cs" />
232+
<Compile Include="DialogOpenedEventArgs.cs" />
233+
<Compile Include="DialogOpenedEventHandler.cs" />
234+
<Compile Include="DialogSession.cs" />
232235
<Compile Include="DialogHost.cs" />
233236
<Compile Include="DrawerHost.cs" />
234237
<Compile Include="ListSortDirectionIndicator.cs" />

0 commit comments

Comments
 (0)