From 68a826e66ffda31486428f6e91a514999b979a5e Mon Sep 17 00:00:00 2001 From: Antony Male Date: Tue, 20 Jan 2015 10:24:12 +0000 Subject: [PATCH] WindowManager does not set Title binding if Title has a value already --- Stylet/WindowManager.cs | 2 +- StyletUnitTests/WindowManagerTests.cs | 39 ++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/Stylet/WindowManager.cs b/Stylet/WindowManager.cs index 321f5b6..a3260a6 100644 --- a/Stylet/WindowManager.cs +++ b/Stylet/WindowManager.cs @@ -130,7 +130,7 @@ namespace Stylet } var haveDisplayName = viewModel as IHaveDisplayName; - if (haveDisplayName != null && BindingOperations.GetBindingBase(window, Window.TitleProperty) == null) + if (haveDisplayName != null && String.IsNullOrEmpty(window.Title) && BindingOperations.GetBindingBase(window, Window.TitleProperty) == null) { var binding = new Binding("DisplayName") { Mode = BindingMode.TwoWay }; window.SetBinding(Window.TitleProperty, binding); diff --git a/StyletUnitTests/WindowManagerTests.cs b/StyletUnitTests/WindowManagerTests.cs index e8987e0..426f286 100644 --- a/StyletUnitTests/WindowManagerTests.cs +++ b/StyletUnitTests/WindowManagerTests.cs @@ -101,10 +101,41 @@ namespace StyletUnitTests this.windowManager.CreateWindow(model, false); var e = window.GetBindingExpression(Window.TitleProperty); + Assert.NotNull(e); Assert.AreEqual(BindingMode.TwoWay, e.ParentBinding.Mode); Assert.AreEqual("DisplayName", e.ParentBinding.Path.Path); } + [Test] + public void CreateWindowDoesNotSetUpTitleBindingIfTitleHasAValueAlready() + { + var model = new Screen(); + var window = new Window(); + window.Title = "Foo"; + this.viewManager.Setup(x => x.CreateAndBindViewForModel(model)).Returns(window); + + this.windowManager.CreateWindow(model, false); + + var e = window.GetBindingExpression(Window.TitleProperty); + Assert.IsNull(e); + Assert.AreEqual("Foo", window.Title); + } + + [Test] + public void CreateWindowDoesNotSetUpTitleBindingIfTitleHasABindingAlready() + { + var model = new Screen(); + var window = new Window(); + var binding = new Binding("Test") { Mode = BindingMode.TwoWay }; + window.SetBinding(Window.TitleProperty, binding); + this.viewManager.Setup(x => x.CreateAndBindViewForModel(model)).Returns(window); + + this.windowManager.CreateWindow(model, false); + + var e = window.GetBindingExpression(Window.TitleProperty); + Assert.AreEqual("Test", e.ParentBinding.Path.Path); + } + [Test] public void CreateWindowActivatesViewModel() { @@ -285,7 +316,7 @@ namespace StyletUnitTests } [Test] - public void SetsWindowStartupLocationToCenterScreenIfThereIsNoOwnerAndItHasNotBeenSetAlready() + public void CreateWindowSetsWindowStartupLocationToCenterScreenIfThereIsNoOwnerAndItHasNotBeenSetAlready() { var model = new object(); var window = new Window(); @@ -297,7 +328,7 @@ namespace StyletUnitTests } [Test] - public void DoesNotSetStartupLocationIfItIsNotManual() + public void CreateWindowDoesNotSetStartupLocationIfItIsNotManual() { var model = new object(); var window = new Window(); @@ -310,7 +341,7 @@ namespace StyletUnitTests } [Test] - public void DoesNotSetStartupLocationIfLeftSet() + public void CreateWindowDoesNotSetStartupLocationIfLeftSet() { var model = new object(); var window = new Window(); @@ -323,7 +354,7 @@ namespace StyletUnitTests } [Test] - public void DoesNotSetStartupLocationIfTopSet() + public void CreateWindowDoesNotSetStartupLocationIfTopSet() { var model = new object(); var window = new Window();