BindingExtensionTests.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Avalonia.Controls;
  5. using Avalonia.Controls.Presenters;
  6. using Avalonia.Controls.Primitives;
  7. using Avalonia.Controls.Templates;
  8. using Avalonia.Styling;
  9. using Avalonia.UnitTests;
  10. using Xunit;
  11. namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions
  12. {
  13. public class BindingExtensionTests : XamlTestBase
  14. {
  15. [Fact]
  16. public void BindingExtension_Binds_To_Source()
  17. {
  18. using (StyledWindow())
  19. {
  20. var xaml = @"
  21. <Window xmlns='https://github.com/avaloniaui'
  22. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  23. <Window.Resources>
  24. <x:String x:Key='text'>foobar</x:String>
  25. </Window.Resources>
  26. <TextBlock Name='textBlock' Text='{Binding Source={StaticResource text}}'/>
  27. </Window>";
  28. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  29. var textBlock = window.FindControl<TextBlock>("textBlock");
  30. window.Show();
  31. Assert.Equal("foobar", textBlock.Text);
  32. }
  33. }
  34. [Fact]
  35. public void BindingExtension_Binds_To_TargetNullValue()
  36. {
  37. using (StyledWindow())
  38. {
  39. var xaml = @"
  40. <Window xmlns='https://github.com/avaloniaui'
  41. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  42. <Window.Resources>
  43. <x:String x:Key='text'>foobar</x:String>
  44. </Window.Resources>
  45. <TextBlock Name='textBlock' Text='{Binding Foo, TargetNullValue={StaticResource text}}'/>
  46. </Window>";
  47. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  48. var textBlock = window.FindControl<TextBlock>("textBlock");
  49. window.DataContext = new FooBar();
  50. window.Show();
  51. Assert.Equal("foobar", textBlock.Text);
  52. }
  53. }
  54. [Fact]
  55. public void BindingExtension_TargetNullValue_UnsetByDefault()
  56. {
  57. using (StyledWindow())
  58. {
  59. var xaml = @"
  60. <Window xmlns='https://github.com/avaloniaui'
  61. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  62. <TextBlock Name='textBlock' IsVisible='{Binding Foo, Converter={x:Static ObjectConverters.IsNotNull}}'/>
  63. </Window>";
  64. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  65. var textBlock = window.FindControl<TextBlock>("textBlock");
  66. window.DataContext = new FooBar();
  67. window.Show();
  68. Assert.Equal(false, textBlock.IsVisible);
  69. }
  70. }
  71. private class FooBar
  72. {
  73. public object Foo { get; } = null;
  74. }
  75. private IDisposable StyledWindow(params (string, string)[] assets)
  76. {
  77. var services = TestServices.StyledWindow.With(
  78. assetLoader: new MockAssetLoader(assets),
  79. theme: () => new Styles
  80. {
  81. WindowStyle(),
  82. });
  83. return UnitTestApplication.Start(services);
  84. }
  85. private Style WindowStyle()
  86. {
  87. return new Style(x => x.OfType<Window>())
  88. {
  89. Setters =
  90. {
  91. new Setter(
  92. Window.TemplateProperty,
  93. new FuncControlTemplate<Window>((x, scope) =>
  94. new VisualLayerManager
  95. {
  96. Child =
  97. new ContentPresenter
  98. {
  99. Name = "PART_ContentPresenter",
  100. [!ContentPresenter.ContentProperty] = x[!Window.ContentProperty],
  101. }.RegisterInNameScope(scope)
  102. }))
  103. }
  104. };
  105. }
  106. }
  107. }