BindingTests_RelativeSource.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using Avalonia.Controls;
  4. using Avalonia.Markup.Xaml.Data;
  5. using Avalonia.UnitTests;
  6. using Xunit;
  7. namespace Avalonia.Markup.Xaml.UnitTests.Data
  8. {
  9. public class BindingTests_RelativeSource
  10. {
  11. [Fact]
  12. public void Should_Bind_To_First_Ancestor()
  13. {
  14. TextBlock target;
  15. var root = new TestRoot
  16. {
  17. Child = new Decorator
  18. {
  19. Name = "decorator",
  20. Child = target = new TextBlock(),
  21. },
  22. };
  23. var binding = new Binding
  24. {
  25. Path = "Name",
  26. RelativeSource = new RelativeSource
  27. {
  28. AncestorType = typeof(Decorator),
  29. }
  30. };
  31. target.Bind(TextBox.TextProperty, binding);
  32. Assert.Equal("decorator", target.Text);
  33. }
  34. [Fact]
  35. public void Should_Bind_To_Second_Ancestor()
  36. {
  37. TextBlock target;
  38. var root = new TestRoot
  39. {
  40. Child = new Decorator
  41. {
  42. Name = "decorator1",
  43. Child = new Decorator
  44. {
  45. Name = "decorator2",
  46. Child = target = new TextBlock(),
  47. }
  48. },
  49. };
  50. var binding = new Binding
  51. {
  52. Path = "Name",
  53. RelativeSource = new RelativeSource
  54. {
  55. AncestorType = typeof(Decorator),
  56. AncestorLevel = 2,
  57. }
  58. };
  59. target.Bind(TextBox.TextProperty, binding);
  60. Assert.Equal("decorator1", target.Text);
  61. }
  62. [Fact]
  63. public void Should_Bind_To_Derived_Ancestor_Type()
  64. {
  65. TextBlock target;
  66. var root = new TestRoot
  67. {
  68. Child = new Border
  69. {
  70. Name = "border",
  71. Child = target = new TextBlock(),
  72. },
  73. };
  74. var binding = new Binding
  75. {
  76. Path = "Name",
  77. RelativeSource = new RelativeSource
  78. {
  79. AncestorType = typeof(Decorator),
  80. }
  81. };
  82. target.Bind(TextBox.TextProperty, binding);
  83. Assert.Equal("border", target.Text);
  84. }
  85. [Fact]
  86. public void Should_Produce_Null_If_Ancestor_Not_Found()
  87. {
  88. TextBlock target;
  89. var root = new TestRoot
  90. {
  91. Child = new Decorator
  92. {
  93. Name = "decorator",
  94. Child = target = new TextBlock(),
  95. },
  96. };
  97. var binding = new Binding
  98. {
  99. Path = "Name",
  100. RelativeSource = new RelativeSource
  101. {
  102. AncestorType = typeof(Decorator),
  103. AncestorLevel = 2,
  104. }
  105. };
  106. target.Bind(TextBox.TextProperty, binding);
  107. Assert.Equal(null, target.Text);
  108. }
  109. [Fact]
  110. public void Should_Update_When_Detached_And_Attached_To_Visual_Tree()
  111. {
  112. TextBlock target;
  113. Decorator decorator1;
  114. Decorator decorator2;
  115. var root1 = new TestRoot
  116. {
  117. Child = decorator1 = new Decorator
  118. {
  119. Name = "decorator1",
  120. Child = target = new TextBlock(),
  121. },
  122. };
  123. var root2 = new TestRoot
  124. {
  125. Child = decorator2 = new Decorator
  126. {
  127. Name = "decorator2",
  128. },
  129. };
  130. var binding = new Binding
  131. {
  132. Path = "Name",
  133. RelativeSource = new RelativeSource
  134. {
  135. AncestorType = typeof(Decorator),
  136. }
  137. };
  138. target.Bind(TextBox.TextProperty, binding);
  139. Assert.Equal("decorator1", target.Text);
  140. decorator1.Child = null;
  141. Assert.Null(target.Text);
  142. decorator2.Child = target;
  143. Assert.Equal("decorator2", target.Text);
  144. }
  145. }
  146. }