11using System ;
22using System . Collections . Generic ;
3+ using System . ComponentModel ;
34using System . Linq ;
5+ using System . Runtime . CompilerServices ;
46using System . Text ;
57using System . Threading . Tasks ;
68using System . Windows . Controls ;
79using System . Windows . Input ;
10+ using System . Windows . Threading ;
811using MaterialDesignThemes . Wpf ;
912
1013namespace MaterialDesignColors . WpfExample . Domain
1114{
12- public class DialogsViewModel
15+ public class DialogsViewModel : INotifyPropertyChanged
1316 {
17+ public DialogsViewModel ( )
18+ {
19+ //Sample 4
20+ OpenSample4DialogCommand = new AnotherCommandImplementation ( OpenSample4Dialog ) ;
21+ AcceptSample4DialogCommand = new AnotherCommandImplementation ( AcceptSample4Dialog ) ;
22+ CancelSample4DialogCommand = new AnotherCommandImplementation ( CancelSample4Dialog ) ;
23+ }
24+
25+ #region SAMPLE 3
26+
1427 public ICommand RunDialogCommand => new AnotherCommandImplementation ( ExecuteRunDialog ) ;
1528
29+ public ICommand RunExtendedDialogCommand => new AnotherCommandImplementation ( ExecuteRunExtendedDialog ) ;
30+
1631 private async void ExecuteRunDialog ( object o )
1732 {
1833 //let's set up a little MVVM, cos that's what the cool kids are doing:
1934 var view = new SampleDialog
2035 {
2136 DataContext = new object ( )
2237 } ;
23-
38+
2439 //show the dialog
2540 var result = await DialogHost . Show ( view , "RootDialog" , ClosingEventHandler ) ;
2641
@@ -32,5 +47,100 @@ private void ClosingEventHandler(object sender, DialogClosingEventArgs eventArgs
3247 {
3348 Console . WriteLine ( "You can intercept the closing event, and cancel here." ) ;
3449 }
50+
51+ private async void ExecuteRunExtendedDialog ( object o )
52+ {
53+ //let's set up a little MVVM, cos that's what the cool kids are doing:
54+ var view = new SampleDialog
55+ {
56+ DataContext = new object ( )
57+ } ;
58+
59+ //show the dialog
60+ var result = await DialogHost . Show ( view , "RootDialog" , ExtendedClosingEventHandler ) ;
61+
62+ //check the result...
63+ Console . WriteLine ( "Dialog was closed, the CommandParameter used to close it was: " + ( result ?? "NULL" ) ) ;
64+ }
65+
66+ private void ExtendedClosingEventHandler ( object sender , DialogClosingEventArgs eventArgs )
67+ {
68+ if ( ( bool ) eventArgs . Parameter == false ) return ;
69+
70+ //OK, lets cancel the close...
71+ eventArgs . Cancel ( ) ;
72+
73+ //...now, lets update the "session" with some new content!
74+ eventArgs . Session . UpdateContent ( new SampleProgressDialog ( ) ) ;
75+ //note, you can also grab the session when the dialog opens via the DialogOpenedEventHandler
76+
77+ //lets run a fake operation for 3 seconds then close this baby.
78+ Task . Delay ( TimeSpan . FromSeconds ( 3 ) )
79+ . ContinueWith ( ( t , _ ) => eventArgs . Session . Close ( false ) , null ,
80+ TaskScheduler . FromCurrentSynchronizationContext ( ) ) ;
81+ }
82+
83+ #endregion
84+
85+ #region SAMPLE 4
86+
87+ //pretty much ignore all the stuff provided, and manage everything via custom commands and a binding for .IsOpen
88+ public ICommand OpenSample4DialogCommand { get ; }
89+ public ICommand AcceptSample4DialogCommand { get ; }
90+ public ICommand CancelSample4DialogCommand { get ; }
91+
92+ private bool _isSample4DialogOpen ;
93+ private object _sample4Content ;
94+
95+ public bool IsSample4DialogOpen
96+ {
97+ get { return _isSample4DialogOpen ; }
98+ set
99+ {
100+ if ( _isSample4DialogOpen == value ) return ;
101+ _isSample4DialogOpen = value ;
102+ OnPropertyChanged ( ) ;
103+ }
104+ }
105+
106+ public object Sample4Content
107+ {
108+ get { return _sample4Content ; }
109+ set
110+ {
111+ if ( _sample4Content == value ) return ;
112+ _sample4Content = value ;
113+ OnPropertyChanged ( ) ;
114+ }
115+ }
116+
117+ private void OpenSample4Dialog ( object obj )
118+ {
119+ Sample4Content = new Sample4Dialog ( ) ;
120+ IsSample4DialogOpen = true ;
121+ }
122+
123+ private void CancelSample4Dialog ( object obj )
124+ {
125+ IsSample4DialogOpen = false ;
126+ }
127+
128+ private void AcceptSample4Dialog ( object obj )
129+ {
130+ //pretend to do something for 3 seconds, then close
131+ Sample4Content = new SampleProgressDialog ( ) ;
132+ Task . Delay ( TimeSpan . FromSeconds ( 3 ) )
133+ . ContinueWith ( ( t , _ ) => IsSample4DialogOpen = false , null ,
134+ TaskScheduler . FromCurrentSynchronizationContext ( ) ) ;
135+ }
136+
137+ #endregion
138+
139+ public event PropertyChangedEventHandler PropertyChanged ;
140+
141+ protected virtual void OnPropertyChanged ( [ CallerMemberName ] string propertyName = null )
142+ {
143+ PropertyChanged ? . Invoke ( this , new PropertyChangedEventArgs ( propertyName ) ) ;
144+ }
35145 }
36146}
0 commit comments