Browse Source

Added TemplateBinding syntactic sugar.

pull/4/head
Steven Kirk 13 years ago
parent
commit
797cbb433b
  1. 5
      Perspex.UnitTests/Controls/ContentControlTests.cs
  2. 2
      Perspex.UnitTests/Styling/TestControlBase.cs
  3. 2
      Perspex.UnitTests/Styling/TestTemplatedControl.cs
  4. 39
      Perspex/BindingExtensions.cs
  5. 1
      Perspex/Perspex.csproj
  6. 32
      Perspex/PerspexObject.cs
  7. 5
      Perspex/Styling/IStyleable.cs
  8. 10
      Perspex/Themes/Default/ButtonStyle.cs

5
Perspex.UnitTests/Controls/ContentControlTests.cs

@ -71,10 +71,7 @@ namespace Perspex.UnitTests.Controls
Border border = new Border(); Border border = new Border();
border.Background = new Perspex.Media.SolidColorBrush(0xffffffff); border.Background = new Perspex.Media.SolidColorBrush(0xffffffff);
ContentPresenter contentPresenter = new ContentPresenter(); ContentPresenter contentPresenter = new ContentPresenter();
contentPresenter.Bind( contentPresenter.TemplateBinding(parent, ContentPresenter.ContentProperty);
ContentPresenter.ContentProperty,
parent.GetObservable(ContentControl.ContentProperty),
BindingPriority.TemplatedParent);
border.Content = contentPresenter; border.Content = contentPresenter;
return border; return border;
}); });

2
Perspex.UnitTests/Styling/TestControlBase.cs

@ -30,7 +30,7 @@ namespace Perspex.UnitTests.Styling
set; set;
} }
public void Bind(PerspexProperty property, IObservable<object> source, BindingPriority priority = BindingPriority.LocalValue) public IDisposable Bind(PerspexProperty property, IObservable<object> source, BindingPriority priority = BindingPriority.LocalValue)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

2
Perspex.UnitTests/Styling/TestTemplatedControl.cs

@ -38,7 +38,7 @@ namespace Perspex.UnitTests.Styling
throw new NotImplementedException(); throw new NotImplementedException();
} }
public void Bind(PerspexProperty property, IObservable<object> source, BindingPriority priority = BindingPriority.LocalValue) public IDisposable Bind(PerspexProperty property, IObservable<object> source, BindingPriority priority = BindingPriority.LocalValue)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

39
Perspex/BindingExtensions.cs

@ -0,0 +1,39 @@
// -----------------------------------------------------------------------
// <copyright file="BindingExtensions.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Perspex.Controls;
/// <summary>
/// Provides binding utility extension methods.
/// </summary>
public static class BindingExtensions
{
/// <summary>
/// Binds a property in a template to the same property in the templated parent.
/// </summary>
/// <typeparam name="T">The property type.</typeparam>
/// <param name="o">The control in the template.</param>
/// <param name="templatedParent">The templated parent.</param>
/// <param name="property">The property.</param>
/// <returns>
/// A disposable which can be used to terminate the binding.
/// </returns>
public static IDisposable TemplateBinding<T>(
this PerspexObject o,
ITemplatedControl templatedParent,
PerspexProperty<T> property)
{
return o.Bind(property, templatedParent.GetObservable(property), BindingPriority.TemplatedParent);
}
}
}

1
Perspex/Perspex.csproj

@ -69,6 +69,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Application.cs" /> <Compile Include="Application.cs" />
<Compile Include="BindingExtensions.cs" />
<Compile Include="Styling\StyleBinding.cs" /> <Compile Include="Styling\StyleBinding.cs" />
<Compile Include="Classes.cs" /> <Compile Include="Classes.cs" />
<Compile Include="Contract.cs" /> <Compile Include="Contract.cs" />

32
Perspex/PerspexObject.cs

@ -417,6 +417,13 @@ namespace Perspex
this.values.Add(property, v); this.values.Add(property, v);
} }
this.Log().Debug(string.Format(
"Set local value of {0}.{1} (#{2:x8}) to {3}",
this.GetType().Name,
property.Name,
this.GetHashCode(),
value));
v.Clear(priority); v.Clear(priority);
v.Add(Observable.Never<object>().StartWith(value), priority); v.Add(Observable.Never<object>().StartWith(value), priority);
} }
@ -441,7 +448,10 @@ namespace Perspex
/// <param name="property">The property.</param> /// <param name="property">The property.</param>
/// <param name="source">The observable.</param> /// <param name="source">The observable.</param>
/// <param name="priority">The priority of the binding.</param> /// <param name="priority">The priority of the binding.</param>
public void Bind( /// <returns>
/// A disposable which can be used to terminate the binding.
/// </returns>
public IDisposable Bind(
PerspexProperty property, PerspexProperty property,
IObservable<object> source, IObservable<object> source,
BindingPriority priority = BindingPriority.LocalValue) BindingPriority priority = BindingPriority.LocalValue)
@ -449,6 +459,7 @@ namespace Perspex
Contract.Requires<NullReferenceException>(property != null); Contract.Requires<NullReferenceException>(property != null);
PriorityValue v; PriorityValue v;
IObservableDescription description = source as IObservableDescription;
if (!this.values.TryGetValue(property, out v)) if (!this.values.TryGetValue(property, out v))
{ {
@ -461,13 +472,14 @@ namespace Perspex
v.Clear((int)priority); v.Clear((int)priority);
} }
v.Add(source, (int)priority);
this.Log().Debug(string.Format( this.Log().Debug(string.Format(
"Bound value of {0}.{1} (#{2:x8})", "Bound value of {0}.{1} (#{2:x8}) to {3}",
this.GetType().Name, this.GetType().Name,
property.Name, property.Name,
this.GetHashCode())); this.GetHashCode(),
description != null ? description.Description : "[Anonymous]"));
return v.Add(source, (int)priority);
} }
/// <summary> /// <summary>
@ -477,14 +489,17 @@ namespace Perspex
/// <param name="property">The property.</param> /// <param name="property">The property.</param>
/// <param name="source">The observable.</param> /// <param name="source">The observable.</param>
/// <param name="priority">The priority of the binding.</param> /// <param name="priority">The priority of the binding.</param>
public void Bind<T>( /// <returns>
/// A disposable which can be used to terminate the binding.
/// </returns>
public IDisposable Bind<T>(
PerspexProperty<T> property, PerspexProperty<T> property,
IObservable<T> source, IObservable<T> source,
BindingPriority priority = BindingPriority.LocalValue) BindingPriority priority = BindingPriority.LocalValue)
{ {
Contract.Requires<NullReferenceException>(property != null); Contract.Requires<NullReferenceException>(property != null);
this.Bind((PerspexProperty)property, (IObservable<object>)source, priority); return this.Bind((PerspexProperty)property, (IObservable<object>)source, priority);
} }
private PriorityValue CreatePriorityValue(PerspexProperty property) private PriorityValue CreatePriorityValue(PerspexProperty property)
@ -505,10 +520,11 @@ namespace Perspex
this.RaisePropertyChanged(property, oldValue, newValue); this.RaisePropertyChanged(property, oldValue, newValue);
this.Log().Debug(string.Format( this.Log().Debug(string.Format(
"Set value of {0}.{1} (#{2:x8}) to {3}", "Value of {0}.{1} (#{2:x8}) changed from {3} to {4}",
this.GetType().Name, this.GetType().Name,
property.Name, property.Name,
this.GetHashCode(), this.GetHashCode(),
oldValue,
newValue)); newValue));
} }
}); });

5
Perspex/Styling/IStyleable.cs

@ -36,7 +36,10 @@ namespace Perspex.Styling
/// <param name="property">The property.</param> /// <param name="property">The property.</param>
/// <param name="source">The observable.</param> /// <param name="source">The observable.</param>
/// <param name="priority">The priority of the binding.</param> /// <param name="priority">The priority of the binding.</param>
void Bind( /// <returns>
/// A disposable which can be used to terminate the binding.
/// </returns>
IDisposable Bind(
PerspexProperty property, PerspexProperty property,
IObservable<object> source, IObservable<object> source,
BindingPriority priority = BindingPriority.LocalValue); BindingPriority priority = BindingPriority.LocalValue);

10
Perspex/Themes/Default/ButtonStyle.cs

@ -56,16 +56,10 @@ namespace Perspex.Themes.Default
Border border = new Border(); Border border = new Border();
border.Id = "border"; border.Id = "border";
border.Padding = new Thickness(3); border.Padding = new Thickness(3);
border.Bind( border.TemplateBinding(control, Border.BackgroundProperty);
Border.BackgroundProperty,
control.GetObservable(Button.BackgroundProperty),
BindingPriority.TemplatedParent);
ContentPresenter contentPresenter = new ContentPresenter(); ContentPresenter contentPresenter = new ContentPresenter();
contentPresenter.Bind( contentPresenter.TemplateBinding(control, ContentPresenter.ContentProperty);
ContentPresenter.ContentProperty,
control.GetObservable(Button.ContentProperty),
BindingPriority.TemplatedParent);
border.Content = contentPresenter; border.Content = contentPresenter;
return border; return border;

Loading…
Cancel
Save