Browse Source

Support Setters with Bindings.

However Styles with activators (e.g. "Foo.class") not yet supported.
pull/396/head
Steven Kirk 10 years ago
parent
commit
5e0dd8fcd3
  1. 46
      src/Perspex.Styling/Styling/Setter.cs
  2. 1
      tests/Perspex.Styling.UnitTests/Perspex.Styling.UnitTests.csproj
  3. 28
      tests/Perspex.Styling.UnitTests/SetterTests.cs

46
src/Perspex.Styling/Styling/Setter.cs

@ -62,15 +62,53 @@ namespace Perspex.Styling
/// <param name="activator">An optional activator.</param> /// <param name="activator">An optional activator.</param>
public void Apply(IStyle style, IStyleable control, IObservable<bool> activator) public void Apply(IStyle style, IStyleable control, IObservable<bool> 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 else
{ {
var binding = new StyleBinding(activator, Value, style.ToString()); if (activator == null)
control.Bind(Property, binding, BindingPriority.StyleTrigger); {
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);
}
} }
} }

1
tests/Perspex.Styling.UnitTests/Perspex.Styling.UnitTests.csproj

@ -91,6 +91,7 @@
<Compile Include="SelectorTests_Template.cs" /> <Compile Include="SelectorTests_Template.cs" />
<Compile Include="StyleActivatorTests.cs" /> <Compile Include="StyleActivatorTests.cs" />
<Compile Include="StyleBindingTests.cs" /> <Compile Include="StyleBindingTests.cs" />
<Compile Include="SetterTests.cs" />
<Compile Include="StyleTests.cs" /> <Compile Include="StyleTests.cs" />
<Compile Include="TestControlBase.cs" /> <Compile Include="TestControlBase.cs" />
<Compile Include="TestObservable.cs" /> <Compile Include="TestObservable.cs" />

28
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<object>("foo");
var binding = Mock.Of<IBinding>(x => x.CreateSubject(control, TextBlock.TextProperty) == subject);
var style = Mock.Of<IStyle>();
var setter = new Setter(TextBlock.TextProperty, binding);
setter.Apply(style, control, null);
Assert.Equal("foo", control.Text);
}
}
}
Loading…
Cancel
Save