BindingTests_ElementName.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright (c) The Perspex Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using Perspex.Controls;
  4. using Perspex.Markup.Xaml.Data;
  5. using Perspex.UnitTests;
  6. using Xunit;
  7. namespace Perspex.Markup.Xaml.UnitTests.Data
  8. {
  9. public class BindingTests_ElementName
  10. {
  11. [Fact]
  12. public void Should_Bind_To_Element_Path()
  13. {
  14. TextBlock target;
  15. var root = new TestRoot
  16. {
  17. Child = new StackPanel
  18. {
  19. Children = new Controls.Controls
  20. {
  21. new TextBlock
  22. {
  23. Name = "source",
  24. Text = "foo",
  25. },
  26. (target = new TextBlock
  27. {
  28. Name = "target",
  29. })
  30. }
  31. }
  32. };
  33. var binding = new Binding
  34. {
  35. ElementName = "source",
  36. Path = "Text",
  37. };
  38. target.Bind(TextBox.TextProperty, binding);
  39. Assert.Equal("foo", target.Text);
  40. }
  41. [Fact]
  42. public void Should_Bind_To_Element()
  43. {
  44. TextBlock source;
  45. ContentControl target;
  46. var root = new TestRoot
  47. {
  48. Child = new StackPanel
  49. {
  50. Children = new Controls.Controls
  51. {
  52. (source = new TextBlock
  53. {
  54. Name = "source",
  55. Text = "foo",
  56. }),
  57. (target = new ContentControl
  58. {
  59. Name = "target",
  60. })
  61. }
  62. }
  63. };
  64. var binding = new Binding
  65. {
  66. ElementName = "source",
  67. };
  68. target.Bind(ContentControl.ContentProperty, binding);
  69. Assert.Same(source, target.Content);
  70. }
  71. [Fact]
  72. public void Should_Bind_To_Later_Added_Element_Path()
  73. {
  74. TextBlock target;
  75. StackPanel stackPanel;
  76. var root = new TestRoot
  77. {
  78. Child = stackPanel = new StackPanel
  79. {
  80. Children = new Controls.Controls
  81. {
  82. (target = new TextBlock
  83. {
  84. Name = "target",
  85. }),
  86. }
  87. }
  88. };
  89. var binding = new Binding
  90. {
  91. ElementName = "source",
  92. Path = "Text",
  93. };
  94. target.Bind(TextBox.TextProperty, binding);
  95. stackPanel.Children.Add(new TextBlock
  96. {
  97. Name = "source",
  98. Text = "foo",
  99. });
  100. Assert.Equal("foo", target.Text);
  101. }
  102. [Fact]
  103. public void Should_Bind_To_Later_Added_Element()
  104. {
  105. ContentControl target;
  106. StackPanel stackPanel;
  107. var root = new TestRoot
  108. {
  109. Child = stackPanel = new StackPanel
  110. {
  111. Children = new Controls.Controls
  112. {
  113. (target = new ContentControl
  114. {
  115. Name = "target",
  116. }),
  117. }
  118. }
  119. };
  120. var binding = new Binding
  121. {
  122. ElementName = "source",
  123. };
  124. target.Bind(ContentControl.ContentProperty, binding);
  125. var source = new TextBlock
  126. {
  127. Name = "source",
  128. Text = "foo",
  129. };
  130. stackPanel.Children.Add(source);
  131. Assert.Same(source, target.Content);
  132. }
  133. }
  134. }