|
|
@@ -8,85 +8,60 @@ namespace Perspex.Styling
|
|
|
{
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
- using System.Reactive.Linq;
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Defines a style.
|
|
|
+ /// </summary>
|
|
|
public class Style : IStyle
|
|
|
{
|
|
|
+ /// <summary>
|
|
|
+ /// Initializes a new instance of the <see cref="Style"/> class.
|
|
|
+ /// </summary>
|
|
|
public Style()
|
|
|
{
|
|
|
- this.Setters = new List<Setter>();
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Initializes a new instance of the <see cref="Style"/> class.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="selector">The style selector.</param>
|
|
|
public Style(Func<Selector, Selector> selector)
|
|
|
- : this()
|
|
|
{
|
|
|
this.Selector = selector(new Selector());
|
|
|
}
|
|
|
|
|
|
- public Selector Selector
|
|
|
- {
|
|
|
- get;
|
|
|
- set;
|
|
|
- }
|
|
|
+ /// <summary>
|
|
|
+ /// Gets or sets style's selector.
|
|
|
+ /// </summary>
|
|
|
+ public Selector Selector { get; set; }
|
|
|
|
|
|
- public IEnumerable<Setter> Setters
|
|
|
- {
|
|
|
- get;
|
|
|
- set;
|
|
|
- }
|
|
|
+ /// <summary>
|
|
|
+ /// Gets or sets style's setters.
|
|
|
+ /// </summary>
|
|
|
+ public IEnumerable<ISetter> Setters { get; set; } = new List<ISetter>();
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Attaches the style to a control if the style's selector matches.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="control">The control to attach to.</param>
|
|
|
public void Attach(IStyleable control)
|
|
|
{
|
|
|
var description = "Style " + this.Selector.ToString();
|
|
|
var match = this.Selector.Match(control);
|
|
|
|
|
|
- if (match.ImmediateResult.HasValue)
|
|
|
- {
|
|
|
- if (match.ImmediateResult == true)
|
|
|
- {
|
|
|
- foreach (Setter setter in this.Setters)
|
|
|
- {
|
|
|
- if (setter.Source != null && setter.Value != null)
|
|
|
- {
|
|
|
- throw new InvalidOperationException("Cannot set both Source and Value on a Setter.");
|
|
|
- }
|
|
|
-
|
|
|
- if (setter.Source == null)
|
|
|
- {
|
|
|
- control.SetValue(setter.Property, setter.Value, BindingPriority.Style);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- control.Bind(setter.Property, setter.Source, BindingPriority.Style);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
+ if (match.ImmediateResult != false)
|
|
|
{
|
|
|
- foreach (Setter setter in this.Setters)
|
|
|
+ foreach (var setter in this.Setters)
|
|
|
{
|
|
|
- if (setter.Source != null && setter.Value != null)
|
|
|
- {
|
|
|
- throw new InvalidOperationException("Cannot set both Source and Value on a Setter.");
|
|
|
- }
|
|
|
-
|
|
|
- StyleBinding binding;
|
|
|
-
|
|
|
- if (setter.Source == null)
|
|
|
- {
|
|
|
- binding = new StyleBinding(match.ObservableResult, setter.Value, description);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- binding = new StyleBinding(match.ObservableResult, setter.Source, description);
|
|
|
- }
|
|
|
-
|
|
|
- control.Bind(setter.Property, binding, BindingPriority.StyleTrigger);
|
|
|
+ setter.Apply(this, control, match.ObservableResult);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Returns a string representation of the style.
|
|
|
+ /// </summary>
|
|
|
+ /// <returns>A string representation of the style.</returns>
|
|
|
public override string ToString()
|
|
|
{
|
|
|
return "Style: " + this.Selector.ToString();
|