BindingTests.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using Avalonia.Controls;
  6. using Avalonia.Controls.Presenters;
  7. using Avalonia.Data;
  8. using Avalonia.Data.Converters;
  9. using Avalonia.Media;
  10. using Avalonia.Media.Immutable;
  11. using Avalonia.UnitTests;
  12. using Avalonia.VisualTree;
  13. using Xunit;
  14. namespace Avalonia.Markup.Xaml.UnitTests.Data
  15. {
  16. public class BindingTests : XamlTestBase
  17. {
  18. [Fact]
  19. public void Binding_With_Null_Path_Works()
  20. {
  21. using (UnitTestApplication.Start(TestServices.StyledWindow))
  22. {
  23. var xaml = @"
  24. <Window xmlns='https://github.com/avaloniaui'
  25. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  26. <TextBlock Name='textBlock' Text='{Binding}'/>
  27. </Window>";
  28. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  29. var textBlock = window.FindControl<TextBlock>("textBlock");
  30. window.DataContext = "foo";
  31. window.ApplyTemplate();
  32. Assert.Equal("foo", textBlock.Text);
  33. }
  34. }
  35. [Fact]
  36. public void Binding_To_DoNothing_Works()
  37. {
  38. using (UnitTestApplication.Start(TestServices.StyledWindow))
  39. {
  40. var xaml = @"
  41. <Window xmlns='https://github.com/avaloniaui'
  42. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  43. <TextBlock Name='textBlock' Text='{Binding}'/>
  44. </Window>";
  45. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  46. var textBlock = window.FindControl<TextBlock>("textBlock");
  47. window.ApplyTemplate();
  48. window.DataContext = "foo";
  49. Assert.Equal("foo", textBlock.Text);
  50. window.DataContext = BindingOperations.DoNothing;
  51. Assert.Equal("foo", textBlock.Text);
  52. window.DataContext = "bar";
  53. Assert.Equal("bar", textBlock.Text);
  54. }
  55. }
  56. [Fact]
  57. public void MultiBinding_TemplatedParent_Works()
  58. {
  59. using (UnitTestApplication.Start(TestServices.StyledWindow))
  60. {
  61. var xaml = @"
  62. <Window xmlns='https://github.com/avaloniaui'
  63. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  64. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Data;assembly=Avalonia.Markup.Xaml.UnitTests'>
  65. <TextBox Name='textBox' Text='Foo' Watermark='Bar'>
  66. <TextBox.Template>
  67. <ControlTemplate>
  68. <TextPresenter Name='PART_TextPresenter'>
  69. <TextPresenter.Text>
  70. <MultiBinding Converter='{x:Static local:ConcatConverter.Instance}'>
  71. <Binding RelativeSource='{RelativeSource TemplatedParent}' Path='Text'/>
  72. <Binding RelativeSource='{RelativeSource TemplatedParent}' Path='Watermark'/>
  73. </MultiBinding>
  74. </TextPresenter.Text>
  75. </TextPresenter>
  76. </ControlTemplate>
  77. </TextBox.Template>
  78. </TextBox>
  79. </Window>";
  80. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  81. var textBox = window.FindControl<TextBox>("textBox");
  82. window.ApplyTemplate();
  83. textBox.ApplyTemplate();
  84. var target = (TextPresenter)textBox.GetVisualChildren().Single();
  85. Assert.Equal("Foo,Bar", target.Text);
  86. }
  87. }
  88. [Fact]
  89. public void Can_Bind_Brush_to_Hex_String()
  90. {
  91. using (UnitTestApplication.Start(TestServices.StyledWindow))
  92. {
  93. var xaml = @"
  94. <Window xmlns='https://github.com/avaloniaui'
  95. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  96. xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Data;assembly=Avalonia.Markup.Xaml.UnitTests'>
  97. <Border Background='{Binding HexString}'/>
  98. </Window>";
  99. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  100. var border = (Border)window.Content;
  101. window.DataContext = new { HexString = "#ff0000" };
  102. window.ApplyTemplate();
  103. var brush = Assert.IsType<ImmutableSolidColorBrush>(border.Background);
  104. Assert.Equal(Colors.Red, brush.Color);
  105. }
  106. }
  107. }
  108. public class ConcatConverter : IMultiValueConverter
  109. {
  110. public static ConcatConverter Instance { get; } = new ConcatConverter();
  111. public object Convert(IList<object> values, Type targetType, object parameter, CultureInfo culture)
  112. {
  113. return string.Join(",", values);
  114. }
  115. }
  116. }