Browse Source

We can now override the default binding mode.

So make TextBlock and TextBox have different default binding modes.
pull/387/merge
Steven Kirk 11 years ago
parent
commit
bb759a8785
  1. 17
      src/Perspex.Base/PropertyMetadata.cs
  2. 2
      src/Perspex.Controls/TextBlock.cs
  3. 1
      src/Perspex.Controls/TextBox.cs
  4. 19
      tests/Perspex.Controls.UnitTests/TextBlockTests.cs
  5. 19
      tests/Perspex.Controls.UnitTests/TextBoxTests.cs
  6. 25
      tests/Perspex.Markup.Xaml.UnitTests/Data/BindingTests.cs

17
src/Perspex.Base/PropertyMetadata.cs

@ -11,19 +11,28 @@ namespace Perspex
/// </summary>
public class PropertyMetadata
{
private BindingMode _defaultBindingMode;
/// <summary>
/// Initializes a new instance of the <see cref="PropertyMetadata"/> class.
/// </summary>
/// <param name="defaultBindingMode">The default binding mode.</param>
public PropertyMetadata(BindingMode defaultBindingMode = BindingMode.Default)
{
DefaultBindingMode = defaultBindingMode;
_defaultBindingMode = defaultBindingMode;
}
/// <summary>
/// Gets the default binding mode for the property.
/// </summary>
public BindingMode DefaultBindingMode { get; private set; }
public BindingMode DefaultBindingMode
{
get
{
return _defaultBindingMode == BindingMode.Default ?
BindingMode.OneWay : _defaultBindingMode;
}
}
/// <summary>
/// Merges the metadata with the base metadata.
@ -34,9 +43,9 @@ namespace Perspex
PropertyMetadata baseMetadata,
PerspexProperty property)
{
if (DefaultBindingMode == BindingMode.Default)
if (_defaultBindingMode == BindingMode.Default)
{
DefaultBindingMode = baseMetadata.DefaultBindingMode;
_defaultBindingMode = baseMetadata.DefaultBindingMode;
}
}
}

2
src/Perspex.Controls/TextBlock.cs

@ -72,7 +72,7 @@ namespace Perspex.Controls
/// Defines the <see cref="Text"/> property.
/// </summary>
public static readonly StyledProperty<string> TextProperty =
PerspexProperty.Register<TextBlock, string>(nameof(Text), defaultBindingMode: BindingMode.TwoWay);
PerspexProperty.Register<TextBlock, string>(nameof(Text));
/// <summary>
/// Defines the <see cref="TextAlignment"/> property.

1
src/Perspex.Controls/TextBox.cs

@ -71,6 +71,7 @@ namespace Perspex.Controls
static TextBox()
{
FocusableProperty.OverrideDefaultValue(typeof(TextBox), true);
TextProperty.OverrideMetadata<TextBox>(new StyledPropertyMetadata<string>(defaultBindingMode: BindingMode.TwoWay));
}
public TextBox()

19
tests/Perspex.Controls.UnitTests/TextBlockTests.cs

@ -0,0 +1,19 @@
// 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 Perspex.Data;
using Xunit;
namespace Perspex.Controls.UnitTests
{
public class TextBlockTests
{
[Fact]
public void DefaultBindingMode_Should_Be_OneWay()
{
Assert.Equal(
BindingMode.OneWay,
TextBlock.TextProperty.GetMetadata(typeof(TextBlock)).DefaultBindingMode);
}
}
}

19
tests/Perspex.Controls.UnitTests/TextBoxTests.cs

@ -0,0 +1,19 @@
// 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 Perspex.Data;
using Xunit;
namespace Perspex.Controls.UnitTests
{
public class TextBoxTests
{
[Fact]
public void DefaultBindingMode_Should_Be_TwoWay()
{
Assert.Equal(
BindingMode.TwoWay,
TextBox.TextProperty.GetMetadata(typeof(TextBox)).DefaultBindingMode);
}
}
}

25
tests/Perspex.Markup.Xaml.UnitTests/Data/BindingTests.cs

@ -96,20 +96,19 @@ namespace Perspex.Markup.Xaml.UnitTests.Data
[Fact]
public void Default_BindingMode_Should_Be_Used()
{
// Default for TextBox.Text is two-way.
var source = new Source { Foo = "foo" };
var target = new TextBlock { DataContext = source };
var target = new TwoWayBindingTest { DataContext = source };
var binding = new Binding
{
Path = "Foo",
};
target.Bind(TextBox.TextProperty, binding);
target.Bind(TwoWayBindingTest.TwoWayProperty, binding);
Assert.Equal("foo", target.Text);
Assert.Equal("foo", target.TwoWay);
source.Foo = "bar";
Assert.Equal("bar", target.Text);
target.Text = "baz";
Assert.Equal("bar", target.TwoWay);
target.TwoWay = "baz";
Assert.Equal("baz", source.Foo);
}
@ -260,6 +259,20 @@ namespace Perspex.Markup.Xaml.UnitTests.Data
Assert.Equal(2, vm.Bar);
}
private class TwoWayBindingTest : Control
{
public static readonly StyledProperty<string> TwoWayProperty =
PerspexProperty.Register<TwoWayBindingTest, string>(
"TwoWay",
defaultBindingMode: BindingMode.TwoWay);
public string TwoWay
{
get { return GetValue(TwoWayProperty); }
set { SetValue(TwoWayProperty, value); }
}
}
public class Source : ReactiveObject
{
private string _foo;

Loading…
Cancel
Save