From 5e0dd8fcd38fb85b45aa69d4cc3102edbcd01f06 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Mon, 18 Jan 2016 23:39:36 +0100 Subject: [PATCH] Support Setters with Bindings. However Styles with activators (e.g. "Foo.class") not yet supported. --- src/Perspex.Styling/Styling/Setter.cs | 46 +++++++++++++++++-- .../Perspex.Styling.UnitTests.csproj | 1 + .../Perspex.Styling.UnitTests/SetterTests.cs | 28 +++++++++++ 3 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 tests/Perspex.Styling.UnitTests/SetterTests.cs diff --git a/src/Perspex.Styling/Styling/Setter.cs b/src/Perspex.Styling/Styling/Setter.cs index 0db359fa40..e93729cf6e 100644 --- a/src/Perspex.Styling/Styling/Setter.cs +++ b/src/Perspex.Styling/Styling/Setter.cs @@ -62,15 +62,53 @@ namespace Perspex.Styling /// An optional activator. public void Apply(IStyle style, IStyleable control, IObservable activator) { - if (activator == null) + if (Property == null) { - control.SetValue(Property, Value, BindingPriority.Style); + throw new InvalidOperationException("Setter.Property must be set."); + } + + var binding = Value as IBinding; + + if (binding != null) + { + if (activator == null) + { + Bind(control, Property, binding); + } + else + { + throw new NotSupportedException( + "Setter bindings with activators not yet supported."); + } } else { - var binding = new StyleBinding(activator, Value, style.ToString()); - control.Bind(Property, binding, BindingPriority.StyleTrigger); + if (activator == null) + { + control.SetValue(Property, Value, BindingPriority.Style); + } + else + { + var activated = new StyleBinding(activator, Value, style.ToString()); + control.Bind(Property, activated, BindingPriority.StyleTrigger); + } } } + + private void Bind(IStyleable control, PerspexProperty property, IBinding binding) + { + var mode = binding.Mode; + + if (mode == BindingMode.Default) + { + mode = property.DefaultBindingMode; + } + + control.Bind( + property, + binding.CreateSubject(control, property), + mode, + binding.Priority); + } } } diff --git a/tests/Perspex.Styling.UnitTests/Perspex.Styling.UnitTests.csproj b/tests/Perspex.Styling.UnitTests/Perspex.Styling.UnitTests.csproj index cc22e7f257..b9e82d589d 100644 --- a/tests/Perspex.Styling.UnitTests/Perspex.Styling.UnitTests.csproj +++ b/tests/Perspex.Styling.UnitTests/Perspex.Styling.UnitTests.csproj @@ -91,6 +91,7 @@ + diff --git a/tests/Perspex.Styling.UnitTests/SetterTests.cs b/tests/Perspex.Styling.UnitTests/SetterTests.cs new file mode 100644 index 0000000000..c42426e97b --- /dev/null +++ b/tests/Perspex.Styling.UnitTests/SetterTests.cs @@ -0,0 +1,28 @@ +// Copyright (c) The Perspex Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System.Reactive.Subjects; +using Moq; +using Perspex.Controls; +using Perspex.Data; +using Xunit; + +namespace Perspex.Styling.UnitTests +{ + public class SetterTests + { + [Fact] + public void Setter_Should_Apply_Binding_To_Property() + { + var control = new TextBlock(); + var subject = new BehaviorSubject("foo"); + var binding = Mock.Of(x => x.CreateSubject(control, TextBlock.TextProperty) == subject); + var style = Mock.Of(); + var setter = new Setter(TextBlock.TextProperty, binding); + + setter.Apply(style, control, null); + + Assert.Equal("foo", control.Text); + } + } +}