public void BindToDataContext(object dataContext) { if (dataContext == null) { return; } try { var bindingSource = new DataContextChangeSynchronizer.BindingSource(SourcePropertyPath, dataContext); var bindingTarget = new DataContextChangeSynchronizer.BindingTarget(Target, TargetProperty); _changeSynchronizer = new DataContextChangeSynchronizer(bindingSource, bindingTarget, _typeConverterProvider); if (BindingMode == BindingMode.TwoWay) { _changeSynchronizer.StartUpdatingTargetWhenSourceChanges(); _changeSynchronizer.StartUpdatingSourceWhenTargetChanges(); } if (BindingMode == BindingMode.OneWay) { _changeSynchronizer.StartUpdatingTargetWhenSourceChanges(); } if (BindingMode == BindingMode.OneWayToSource) { _changeSynchronizer.StartUpdatingSourceWhenTargetChanges(); } } catch (Exception e) { Debug.WriteLine(e); } }
public void BindToDataContext(object dataContext) { if (dataContext == null) { return; } try { var bindingSource = new DataContextChangeSynchronizer.BindingSource(SourcePropertyPath, dataContext); var bindingTarget = new DataContextChangeSynchronizer.BindingTarget(Target, TargetProperty); _changeSynchronizer = new DataContextChangeSynchronizer(bindingSource, bindingTarget, _typeConverterProvider); if (BindingMode == BindingMode.TwoWay) { _changeSynchronizer.StartUpdatingTargetWhenSourceChanges(); _changeSynchronizer.StartUpdatingSourceWhenTargetChanges(); } if (BindingMode == BindingMode.OneWay) { _changeSynchronizer.StartUpdatingTargetWhenSourceChanges(); } if (BindingMode == BindingMode.OneWayToSource) { _changeSynchronizer.StartUpdatingSourceWhenTargetChanges(); } } catch (Exception e) { Debug.WriteLine(e); } }
public void SameTypesFromModelToUI() { var synchronizer = new DataContextChangeSynchronizer(new DataContextChangeSynchronizer.BindingSource(new PropertyPath("IntProp"), _viewModel), new DataContextChangeSynchronizer.BindingTarget(_guiObject, SamplePerspexObject.IntProperty), _repo); synchronizer.StartUpdatingTargetWhenSourceChanges(); _viewModel.IntProp = 2; Assert.Equal(2, _guiObject.Int); }
public void DifferentTypesAndNonConvertibleValueFromUIToModel() { var synchronizer = new DataContextChangeSynchronizer(new DataContextChangeSynchronizer.BindingSource(new PropertyPath("IntProp"), _viewModel), new DataContextChangeSynchronizer.BindingTarget(_guiObject, SamplePerspexObject.StringProperty), _repo); synchronizer.StartUpdatingSourceWhenTargetChanges(); _guiObject.String = ""; Assert.Equal(default(int), _viewModel.IntProp); }
public void SameTypesFromUIToModel() { var synchronizer = new DataContextChangeSynchronizer(new DataContextChangeSynchronizer.BindingSource(new PropertyPath("IntProp"), _viewModel), new DataContextChangeSynchronizer.BindingTarget(_guiObject, SamplePerspexObject.IntProperty), _repo); synchronizer.StartUpdatingSourceWhenTargetChanges(); const int someValue = 4; _guiObject.Int = someValue; Assert.Equal(someValue, _viewModel.IntProp); }
public void Bind(object dataContext) { if (dataContext == null) { return; } try { if (this.BindingMode == BindingMode.TwoWay) { var changeSynchronizer = new DataContextChangeSynchronizer( this.Target, this.TargetProperty, this.SourcePropertyPath, dataContext, this.typeConverterProvider); changeSynchronizer.SubscribeUIToModel(); changeSynchronizer.SubscribeModelToUI(); } if (this.BindingMode == BindingMode.OneWay) { var subscriptionHandler = new DataContextChangeSynchronizer( this.Target, this.TargetProperty, this.SourcePropertyPath, dataContext, this.typeConverterProvider); subscriptionHandler.SubscribeUIToModel(); } if (this.BindingMode == BindingMode.OneWayToSource) { var subscriptionHandler = new DataContextChangeSynchronizer(this.Target, this.TargetProperty, this.SourcePropertyPath, dataContext, this.typeConverterProvider); subscriptionHandler.SubscribeModelToUI(); } } catch (Exception e) { Debug.WriteLine(e); } }
public void GrokysTest() { var mainWindowViewModel = new MainWindowViewModel(); var contentControl = new ContentControl(); var synchronizer = new DataContextChangeSynchronizer(new DataContextChangeSynchronizer.BindingSource(new PropertyPath("Content"), mainWindowViewModel), new DataContextChangeSynchronizer.BindingTarget(contentControl, ContentControl.ContentProperty), _repo); synchronizer.StartUpdatingTargetWhenSourceChanges(); var logInViewModel = new LogInViewModel(); mainWindowViewModel.Content = logInViewModel; Assert.Equal(logInViewModel, contentControl.Content); }