Behavior that synchronizes the IRegion.Context property of a IRegion with the control that hosts the Region. It does this by setting the RegionManager.RegionContextProperty Dependency Property on the host control. This behavior allows the usage of two way databinding of the RegionContext from XAML.
Наследование: RegionBehavior, IHostAwareRegionBehavior
        public void AttachShouldNotThrowWhenHostControlNull()
        {
            MockPresentationRegion region = new MockPresentationRegion();

            SyncRegionContextWithHostBehavior behavior = new SyncRegionContextWithHostBehavior();
            behavior.Region = region;
            behavior.Attach();
        }
        public void ShouldGetInitialValueFromHostAndSetOnRegion()
        {
            MockPresentationRegion region = new MockPresentationRegion();

            SyncRegionContextWithHostBehavior behavior = new SyncRegionContextWithHostBehavior();
            behavior.Region = region;
            DependencyObject mockDependencyObject = new MockDependencyObject();
            behavior.HostControl = mockDependencyObject;

            RegionContext.GetObservableContext(mockDependencyObject).Value = "NewValue";

            Assert.IsNull(region.Context);
            behavior.Attach();
            Assert.AreEqual("NewValue", region.Context);

        }
        public async Task ShouldGetInitialValueFromHostAndSetOnRegion()
        {
            await ExecuteOnUIThread(() =>
                {
                    MockPresentationRegion region = new MockPresentationRegion();

                    SyncRegionContextWithHostBehavior behavior = new SyncRegionContextWithHostBehavior() { Region = region };
                    DependencyObject mockDependencyObject = new MockDependencyObject();
                    behavior.HostControl = mockDependencyObject;

                    RegionContext.GetObservableContext(mockDependencyObject).Value = "NewValue";

                    Assert.IsNull(region.Context);
                    behavior.Attach();
                    Assert.AreEqual("NewValue", region.Context);
                });
        }
        public void ShouldUpdateHostControlRegionContextValueWhenContextOfRegionChanges()
        {
            MockPresentationRegion region = new MockPresentationRegion();

            SyncRegionContextWithHostBehavior behavior = new SyncRegionContextWithHostBehavior();
            behavior.Region = region;
            DependencyObject mockDependencyObject = new MockDependencyObject();
            behavior.HostControl = mockDependencyObject;

            ObservableObject<object> observableRegionContext = RegionContext.GetObservableContext(mockDependencyObject);

            behavior.Attach();
            Assert.IsNull(observableRegionContext.Value);
            region.Context = "NewValue";

            Assert.AreEqual("NewValue", observableRegionContext.Value);

        }
        public void ChangingRegionContextObservableObjectValueShouldAlsoChangeRegionContextDependencyProperty()
        {
            MockPresentationRegion region = new MockPresentationRegion();

            SyncRegionContextWithHostBehavior behavior = new SyncRegionContextWithHostBehavior();
            behavior.Region = region;
            DependencyObject hostControl = new MockDependencyObject();
            behavior.HostControl = hostControl;

            behavior.Attach();

            Assert.IsNull(RegionManager.GetRegionContext(hostControl));
            RegionContext.GetObservableContext(hostControl).Value = "NewValue";

            Assert.AreEqual("NewValue", RegionManager.GetRegionContext(hostControl));
        }
        public void SettingHostControlAfterAttachThrows()
        {

            SyncRegionContextWithHostBehavior behavior = new SyncRegionContextWithHostBehavior();
            DependencyObject hostControl1 = new MockDependencyObject();
            behavior.HostControl = hostControl1;

            behavior.Attach();
            DependencyObject hostControl2 = new MockDependencyObject();
            behavior.HostControl = hostControl2;
        }
        public async Task SettingHostControlAfterAttachThrows()
        {
            await ExecuteOnUIThread(() =>
                {
                    SyncRegionContextWithHostBehavior behavior = new SyncRegionContextWithHostBehavior();
                    DependencyObject hostControl1 = new MockDependencyObject();
                    behavior.HostControl = hostControl1;

                    behavior.Attach();
                    DependencyObject hostControl2 = new MockDependencyObject();
                    Assert.ThrowsException<InvalidOperationException>(() => behavior.HostControl = hostControl2);
                });
        }
        public async Task AttachShouldChangeRegionContextDependencyProperty()
        {
            await ExecuteOnUIThread(() =>
                {
                    MockPresentationRegion region = new MockPresentationRegion();

                    SyncRegionContextWithHostBehavior behavior = new SyncRegionContextWithHostBehavior() { Region = region };
                    DependencyObject hostControl = new MockDependencyObject();
                    behavior.HostControl = hostControl;

                    RegionContext.GetObservableContext(hostControl).Value = "NewValue";

                    Assert.IsNull(RegionManager.GetRegionContext(hostControl));
                    behavior.Attach();
                    Assert.AreEqual("NewValue", RegionManager.GetRegionContext(hostControl));
                });
        }
        public void AttachShouldNotThrowWhenHostControlNullAndRegionContextSet()
        {
            MockPresentationRegion region = new MockPresentationRegion();

            SyncRegionContextWithHostBehavior behavior = new SyncRegionContextWithHostBehavior() { Region = region };
            behavior.Attach();
            region.Context = "Changed";
        }