Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Eyeball it.</strong><br> This kind of declarative markup rarely breaks.. unless someone goes in manual and screws it up. Even then, you can fix it within minutes. IMHO the cost of writing such tests far outweigh the benefits.</p> <p><strong>Update</strong>[Dec3,08]: Alrighty then.<br> The test is just testing that the textbox has the value "FirstName" as the Path property of the binding. If I change/refactor FirstName to JustName in the actual data source object, the test would still pass since it is testing against an anonymous type. (Green test when code broken - TDD Antipattern: The Liar) If your aim is to verify that FirstName has been specified in XAML,</p> <pre><code>Assert.AreEqual("FirstName", txtBoxToProbe.GetBindingExpression(TextBox.TextProperty).ParentBinding.Path.Path); </code></pre> <p>If you really must catch broken bindings via unit tests (and don't want to show the UI), use the real data source... struggled for a while and came up with this.</p> <pre><code>[Test] public void TestTextBoxBinding() { MyWindow w = new MyWindow(); TextBox txtBoxToProbe = w.TextBox1; Object obDataSource = w; // use 'real' data source BindingExpression bindingExpr = BindingOperations.GetBindingExpression(txtBoxToProbe, TextBox.TextProperty); Binding newBind = new Binding(bindingExpr.ParentBinding.Path.Path); newBind.Source = obDataSource; txtBoxToProbe.SetBinding(TextBox.TextProperty, newBind); Assert.AreEqual("Go ahead. Change my value.", txtBoxToProbe.Text); } </code></pre> <p>Epilogue: There's some <a href="http://social.expression.microsoft.com/Forums/en-US/wpf/thread/9b5b76e1-9a9f-4c24-bfd7-ebb5659a097e/" rel="nofollow noreferrer">real covert stuff</a> happening in the call to <code>Window.Show()</code>. It somehow magically sets up the DataItem property after which data binding starts working.</p> <pre><code>// before show bindingExpr.DataItem =&gt; null bindingExpr.Status =&gt; BindingStatus.Unattached // after show bindingExpr.DataItem =&gt; {Actual Data Source} bindingExpr.Status =&gt; BindingStatus.Active </code></pre> <p>Once the Binding is Active, I guess you can force textbox updates via code like this..</p> <pre><code>txtBoxToProbe.GetBindingExpression(TextBox.TextProperty).UpdateTarget(); </code></pre> <p>Once again, I voice my reluctance against this approach. Getting NUnit to run in STA was a pain..</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload