@@ -10,7 +10,7 @@ namespace Eto.Forms
10
10
/// </summary>
11
11
public class CheckCommand : Command , IValueCommand < bool >
12
12
{
13
- #region Events
13
+ #region Events
14
14
15
15
/// <summary>
16
16
/// Occurs when the <see cref="Checked"/> value has changed.
@@ -27,9 +27,9 @@ protected virtual void OnCheckedChanged(EventArgs e)
27
27
CheckedChanged ( this , e ) ;
28
28
}
29
29
30
- #endregion
30
+ #endregion
31
31
32
- #region Properties
32
+ #region Properties
33
33
34
34
bool ischecked ;
35
35
@@ -50,7 +50,7 @@ public bool Checked
50
50
}
51
51
}
52
52
53
- #endregion
53
+ #endregion
54
54
55
55
/// <summary>
56
56
/// Initializes a new instance of the <see cref="Eto.Forms.CheckCommand"/> class.
@@ -157,7 +157,7 @@ public override ToolItem CreateToolItem()
157
157
/// </remarks>
158
158
public class Command : IBindable , ICommand
159
159
{
160
- #region Events
160
+ #region Events
161
161
162
162
/// <summary>
163
163
/// Occurs when the <see cref="Enabled"/> property is changed.
@@ -189,9 +189,9 @@ protected virtual void OnExecuted(EventArgs e)
189
189
Executed ( this , e ) ;
190
190
}
191
191
192
- #endregion
192
+ #endregion
193
193
194
- #region Properties
194
+ #region Properties
195
195
196
196
/// <summary>
197
197
/// Gets or sets the ID of the command
@@ -261,7 +261,7 @@ public virtual bool Enabled
261
261
/// <value>The command shortcut.</value>
262
262
public Keys Shortcut { get ; set ; }
263
263
264
- #endregion
264
+ #endregion
265
265
266
266
/// <summary>
267
267
/// Initializes a new instance of the <see cref="Eto.Forms.Command"/> class.
@@ -329,8 +329,8 @@ public static implicit operator ToolItem(Command command)
329
329
/// Gets the dictionary of properties for this widget
330
330
/// </summary>
331
331
public PropertyStore Properties
332
- {
333
- get { return properties ?? ( properties = new PropertyStore ( this ) ) ; }
332
+ {
333
+ get { return properties ?? ( properties = new PropertyStore ( this ) ) ; }
334
334
}
335
335
336
336
static readonly object Command_Key = new object ( ) ;
@@ -378,7 +378,7 @@ event EventHandler ICommand.CanExecuteChanged
378
378
}
379
379
380
380
bool ICommand . CanExecute ( object parameter )
381
- {
381
+ {
382
382
return Enabled ;
383
383
}
384
384
@@ -387,7 +387,7 @@ void ICommand.Execute(object parameter)
387
387
Execute ( ) ;
388
388
}
389
389
390
- #region IBindable implementation
390
+ #region IBindable implementation
391
391
392
392
/// <summary>
393
393
/// Event to handle when the <see cref="DataContext"/> has changed
@@ -423,7 +423,7 @@ public IBindable Parent
423
423
{
424
424
get { return Properties . Get < IBindable > ( Parent_Key ) ; }
425
425
set
426
- {
426
+ {
427
427
var old = Parent ;
428
428
Properties . Set ( Parent_Key , value , ( ) =>
429
429
{
@@ -432,7 +432,11 @@ public IBindable Parent
432
432
if ( value != null )
433
433
value . DataContextChanged += Value_DataContextChanged ;
434
434
if ( ! Properties . ContainsKey ( DataContext_Key ) )
435
+ {
436
+ IsDataContextChanging = true ;
435
437
OnDataContextChanged ( EventArgs . Empty ) ;
438
+ IsDataContextChanging = false ;
439
+ }
436
440
} ) ;
437
441
}
438
442
}
@@ -462,10 +466,36 @@ public object DataContext
462
466
return bindable != null ? bindable . DataContext : null ;
463
467
} ) ;
464
468
}
465
- set { Properties . Set ( DataContext_Key , value , ( ) => OnDataContextChanged ( EventArgs . Empty ) ) ; }
469
+ set
470
+ {
471
+ if ( Properties . TrySet ( DataContext_Key , value ) )
472
+ {
473
+ IsDataContextChanging = true ;
474
+ OnDataContextChanged ( EventArgs . Empty ) ;
475
+ IsDataContextChanging = false ;
476
+ }
477
+ }
478
+ }
479
+
480
+ static readonly object IsDataContextChanging_Key = new object ( ) ;
481
+
482
+ /// <summary>
483
+ /// Gets a value indicating that the <see cref="DataContext"/> property is changing.
484
+ /// </summary>
485
+ /// <remarks>
486
+ /// This can be used to determine when to allow certain logic during the update of the data context.
487
+ ///
488
+ /// It is used to disable binding setters on the model when the data context changes so that a binding
489
+ /// does not cause the view model to be updated when the state hasn't been fully set yet.
490
+ /// </remarks>
491
+ /// <value><c>true</c> if the DataContext is currently changing, <c>false</c> otherwise.</value>
492
+ public bool IsDataContextChanging
493
+ {
494
+ get => Properties . Get < bool ? > ( IsDataContextChanging_Key ) ?? ( Parent as IBindable ) ? . IsDataContextChanging ?? false ;
495
+ set => Properties . Set ( IsDataContextChanging_Key , value ) ;
466
496
}
467
497
468
- static readonly object Bindings_Key = new object ( ) ;
498
+ static readonly object Bindings_Key = new object ( ) ;
469
499
470
500
/// <summary>
471
501
/// Gets the collection of bindings that are attached to this widget
@@ -475,6 +505,6 @@ public BindingCollection Bindings
475
505
get { return Properties . Create ( Bindings_Key , ( ) => new BindingCollection ( ) ) ; }
476
506
}
477
507
478
- #endregion
508
+ #endregion
479
509
}
480
510
}
0 commit comments