BindingExtensionTests.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 loader = new AvaloniaXamlLoader();
  29. var window = (Window)loader.Load(xaml);
  30. var textBlock = window.FindControl<TextBlock>("textBlock");
  31. window.Show();
  32. Assert.Equal("foobar", textBlock.Text);
  33. }
  34. }
  35. [Fact]
  36. public void BindingExtension_Binds_To_TargetNullValue()
  37. {
  38. using (StyledWindow())
  39. {
  40. var xaml = @"
  41. <Window xmlns='https://github.com/avaloniaui'
  42. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  43. <Window.Resources>
  44. <x:String x:Key='text'>foobar</x:String>
  45. </Window.Resources>
  46. <TextBlock Name='textBlock' Text='{Binding Foo, TargetNullValue={StaticResource text}}'/>
  47. </Window>";
  48. var loader = new AvaloniaXamlLoader();
  49. var window = (Window)loader.Load(xaml);
  50. var textBlock = window.FindControl<TextBlock>("textBlock");
  51. window.DataContext = new FooBar();
  52. window.Show();
  53. Assert.Equal("foobar", textBlock.Text);
  54. }
  55. }
  56. [Fact]
  57. public void BindingExtension_TargetNullValue_UnsetByDefault()
  58. {
  59. using (StyledWindow())
  60. {
  61. var xaml = @"
  62. <Window xmlns='https://github.com/avaloniaui'
  63. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  64. <TextBlock Name='textBlock' IsVisible='{Binding Foo, Converter={x:Static ObjectConverters.IsNotNull}}'/>
  65. </Window>";
  66. var loader = new AvaloniaXamlLoader();
  67. var window = (Window)loader.Load(xaml);
  68. var textBlock = window.FindControl<TextBlock>("textBlock");
  69. window.DataContext = new FooBar();
  70. window.Show();
  71. Assert.Equal(false, textBlock.IsVisible);
  72. }
  73. }
  74. private class FooBar
  75. {
  76. public object Foo { get; } = null;
  77. }
  78. private IDisposable StyledWindow(params (string, string)[] assets)
  79. {
  80. var services = TestServices.StyledWindow.With(
  81. assetLoader: new MockAssetLoader(assets),
  82. theme: () => new Styles
  83. {
  84. WindowStyle(),
  85. });
  86. return UnitTestApplication.Start(services);
  87. }
  88. private Style WindowStyle()
  89. {
  90. return new Style(x => x.OfType<Window>())
  91. {
  92. Setters =
  93. {
  94. new Setter(
  95. Window.TemplateProperty,
  96. new FuncControlTemplate<Window>((x, scope) =>
  97. new VisualLayerManager
  98. {
  99. Child =
  100. new ContentPresenter
  101. {
  102. Name = "PART_ContentPresenter",
  103. [!ContentPresenter.ContentProperty] = x[!Window.ContentProperty],
  104. }.RegisterInNameScope(scope)
  105. }))
  106. }
  107. };
  108. }
  109. }
  110. }