csharpc-sharpdotnetxamlavaloniauicross-platformcross-platform-xamlavaloniaguimulti-platformuser-interfacedotnetcore
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
3.5 KiB
131 lines
3.5 KiB
using System;
|
|
using System.Globalization;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Data;
|
|
using Avalonia.Data.Converters;
|
|
using Avalonia.Data.Core;
|
|
using Avalonia.UnitTests;
|
|
using Moq;
|
|
using Xunit;
|
|
|
|
namespace Avalonia.Markup.UnitTests.Data
|
|
{
|
|
public class BindingTests_Converters : ScopedTestBase
|
|
{
|
|
[Fact]
|
|
public void Converter_Should_Be_Used()
|
|
{
|
|
var textBlock = new TextBlock
|
|
{
|
|
DataContext = new Class1(),
|
|
};
|
|
|
|
var target = new Binding(nameof(Class1.Foo))
|
|
{
|
|
Converter = StringConverters.IsNullOrEmpty,
|
|
};
|
|
|
|
var expressionObserver = (BindingExpression)target.Initiate(
|
|
textBlock,
|
|
TextBlock.TextProperty).Expression;
|
|
|
|
Assert.Same(StringConverters.IsNullOrEmpty, 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.IsNotNullOrEmpty,
|
|
StringFormat = "Hello {0}",
|
|
};
|
|
|
|
textBlock.Bind(TextBlock.TextProperty, target);
|
|
|
|
Assert.Equal("Hello True", textBlock.Text);
|
|
}
|
|
|
|
[Fact]
|
|
public void ConverterCulture_Should_Be_Passed_To_Converter_Convert()
|
|
{
|
|
var textBlock = new TextBlock
|
|
{
|
|
DataContext = new Class1(),
|
|
};
|
|
|
|
var culture = new CultureInfo("ar-SA");
|
|
var converter = new Mock<IValueConverter>();
|
|
var target = new Binding(nameof(Class1.Foo))
|
|
{
|
|
Converter = converter.Object,
|
|
ConverterCulture = culture,
|
|
};
|
|
|
|
textBlock.Bind(TextBlock.TextProperty, target);
|
|
|
|
converter.Verify(converter => converter.Convert(
|
|
"foo",
|
|
typeof(string),
|
|
null,
|
|
culture),
|
|
Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public void ConverterCulture_Should_Be_Passed_To_Converter_ConvertBack()
|
|
{
|
|
var textBlock = new TextBlock
|
|
{
|
|
DataContext = new Class1(),
|
|
};
|
|
|
|
var culture = new CultureInfo("ar-SA");
|
|
var converter = new Mock<IValueConverter>();
|
|
var target = new Binding(nameof(Class1.Foo))
|
|
{
|
|
Converter = converter.Object,
|
|
ConverterCulture = culture,
|
|
Mode = BindingMode.TwoWay,
|
|
};
|
|
|
|
textBlock.Bind(TextBlock.TextProperty, target);
|
|
textBlock.Text = "bar";
|
|
|
|
converter.Verify(converter => converter.ConvertBack(
|
|
"bar",
|
|
typeof(string),
|
|
null,
|
|
culture),
|
|
Times.Once);
|
|
}
|
|
|
|
private class Class1
|
|
{
|
|
public string Foo { get; set; } = "foo";
|
|
}
|
|
}
|
|
}
|
|
|