Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add StringFormat to Binding. #1684

Merged
merged 8 commits into from
Aug 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/Avalonia.Base/Data/Converters/StringFormatValueConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Globalization;

namespace Avalonia.Data.Converters
{
/// <summary>
/// A value converter which calls <see cref="string.Format(string, object)"/>
/// </summary>
public class StringFormatValueConverter : IValueConverter
{
/// <summary>
/// Initializes a new instance of the <see cref="StringFormatValueConverter"/> class.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="inner">
/// An optional inner converter to be called before the format takes place.
/// </param>
public StringFormatValueConverter(string format, IValueConverter inner)
{
Contract.Requires<ArgumentNullException>(format != null);

Format = format;
Inner = inner;
}

/// <summary>
/// Gets an inner value converter which will be called before the string format takes place.
/// </summary>
public IValueConverter Inner { get; }

/// <summary>
/// Gets the format string.
/// </summary>
public string Format { get; }

/// <inheritdoc/>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
value = Inner?.Convert(value, targetType, parameter, culture) ?? value;
return string.Format(culture, Format, value);
}

/// <inheritdoc/>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException("Two way bindings are not supported with a string format");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ public override object ProvideValue(IServiceProvider serviceProvider)
Path = Path,
Priority = Priority,
Source = Source,
StringFormat = StringFormat,
RelativeSource = RelativeSource,
DefaultAnchor = new WeakReference(GetDefaultAnchor((ITypeDescriptorContext)serviceProvider))
DefaultAnchor = new WeakReference(GetDefaultAnchor(descriptorContext))
};
}

Expand Down Expand Up @@ -79,6 +80,8 @@ private static object GetDefaultAnchor(ITypeDescriptorContext context)

public object Source { get; set; }

public string StringFormat { get; set; }

public RelativeSource RelativeSource { get; set; }
}
}
21 changes: 19 additions & 2 deletions src/Markup/Avalonia.Markup/Data/Binding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ public Binding(string path, BindingMode mode = BindingMode.Default)
/// </summary>
public object Source { get; set; }

/// <summary>
/// Gets or sets the string format.
/// </summary>
public string StringFormat { get; set; }

public WeakReference DefaultAnchor { get; set; }

/// <summary>
Expand Down Expand Up @@ -181,11 +186,23 @@ public InstancedBinding Initiate(
fallback = null;
}

var converter = Converter;
var targetType = targetProperty?.PropertyType ?? typeof(object);

// We only respect `StringFormat` if the type of the property we're assigning to will
// accept a string. Note that this is slightly different to WPF in that WPF only applies
// `StringFormat` for target type `string` (not `object`).
if (!string.IsNullOrWhiteSpace(StringFormat) &&
(targetType == typeof(string) || targetType == typeof(object)))
{
converter = new StringFormatValueConverter(StringFormat, converter);
}

var subject = new BindingExpression(
observer,
targetProperty?.PropertyType ?? typeof(object),
targetType,
fallback,
Converter ?? DefaultValueConverter.Instance,
converter ?? DefaultValueConverter.Instance,
ConverterParameter,
Priority);

Expand Down
146 changes: 146 additions & 0 deletions tests/Avalonia.Markup.UnitTests/Data/BindingTests_Converters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.

using System;
using Avalonia.Controls;
using Avalonia.Data;
using Avalonia.Data.Converters;
using Avalonia.Data.Core;
using Xunit;

namespace Avalonia.Markup.UnitTests.Data
{
public class BindingTests_Converters
{
[Fact]
public void Converter_Should_Be_Used()
{
var textBlock = new TextBlock
{
DataContext = new Class1(),
};

var target = new Binding(nameof(Class1.Foo))
{
Converter = StringConverters.NullOrEmpty,
};

var expressionObserver = (BindingExpression)target.Initiate(
textBlock,
TextBlock.TextProperty).Observable;

Assert.Same(StringConverters.NullOrEmpty, expressionObserver.Converter);
}

public class When_Binding_To_String
{
[Fact]
public void StringFormatConverter_Should_Be_Used_When_Binding_Has_StringFormat()
{
var textBlock = new TextBlock
{
DataContext = new Class1(),
};

var target = new Binding(nameof(Class1.Foo))
{
StringFormat = "Hello {0}",
};

var expressionObserver = (BindingExpression)target.Initiate(
textBlock,
TextBlock.TextProperty).Observable;

Assert.IsType<StringFormatValueConverter>(expressionObserver.Converter);
}
}

public class When_Binding_To_Object
{
[Fact]
public void StringFormatConverter_Should_Be_Used_When_Binding_Has_StringFormat()
{
var textBlock = new TextBlock
{
DataContext = new Class1(),
};

var target = new Binding(nameof(Class1.Foo))
{
StringFormat = "Hello {0}",
};

var expressionObserver = (BindingExpression)target.Initiate(
textBlock,
TextBlock.TagProperty).Observable;

Assert.IsType<StringFormatValueConverter>(expressionObserver.Converter);
}
}

public class When_Binding_To_Non_String_Or_Object
{
[Fact]
public void StringFormatConverter_Should_Not_Be_Used_When_Binding_Has_StringFormat()
{
var textBlock = new TextBlock
{
DataContext = new Class1(),
};

var target = new Binding(nameof(Class1.Foo))
{
StringFormat = "Hello {0}",
};

var expressionObserver = (BindingExpression)target.Initiate(
textBlock,
TextBlock.MarginProperty).Observable;

Assert.Same(DefaultValueConverter.Instance, expressionObserver.Converter);
}
}

[Fact]
public void StringFormat_Should_Be_Applied()
{
var textBlock = new TextBlock
{
DataContext = new Class1(),
};

var target = new Binding(nameof(Class1.Foo))
{
StringFormat = "Hello {0}",
};

textBlock.Bind(TextBlock.TextProperty, target);

Assert.Equal("Hello foo", textBlock.Text);
}

[Fact]
public void StringFormat_Should_Be_Applied_After_Converter()
{
var textBlock = new TextBlock
{
DataContext = new Class1(),
};

var target = new Binding(nameof(Class1.Foo))
{
Converter = StringConverters.NotNullOrEmpty,
StringFormat = "Hello {0}",
};

textBlock.Bind(TextBlock.TextProperty, target);

Assert.Equal("Hello True", textBlock.Text);
}

private class Class1
{
public string Foo { get; set; } = "foo";
}
}
}
22 changes: 22 additions & 0 deletions tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BindingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,5 +308,27 @@ public void Binding_To_Attached_Property_In_Style_Works()
Assert.Equal(5.6, AttachedPropertyOwner.GetDouble(textBlock));
}
}

[Fact]
public void Binding_To_TextBlock_Text_With_StringConverter_Works()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var xaml = @"
<Window xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
<TextBlock Name='textBlock' Text='{Binding Foo, StringFormat=Hello \{0\}}'/>
</Window>";
var loader = new AvaloniaXamlLoader();
var window = (Window)loader.Load(xaml);
var textBlock = window.FindControl<TextBlock>("textBlock");

textBlock.DataContext = new { Foo = "world" };
window.ApplyTemplate();

Assert.Equal("Hello world", textBlock.Text);
}
}
}
}