我想單元測試我的WPF數據綁定,使用Microsoft Team System提供的測試套裝。我希望能夠在不顯示窗口的情況下測試綁定,因爲我的大多數測試都是針對用戶控件,而不是實際上在窗口上。這是可能的還是有更好的方法來做到這一點?下面的代碼工作,如果我顯示窗口,但如果我不這樣做,綁定不會更新。單元測試WPF綁定
Window1_Accessor target = new Window1_Accessor();
UnitTestingWPF.Window1_Accessor.Person p = new UnitTestingWPF.Window1_Accessor.Person() { FirstName = "Shane" };
Window1 window = (target.Target as Window1);
window.DataContext = p;
//window.Show(); //Only Works when I actually show the window
//Is it possible to manually update the binding here, maybe? Is there a better way?
Assert.AreEqual("Shane", target.textBoxFirstName.Text); //Fails if I don't Show() the window because the bindings aren't updated
如果我們在類結合的特性,並重構類,XAML中仍然會編譯,但沒有將引發異常,我們的應用程序將無法再正常的綁定將是不正確的。這對我們來說是一個問題,這就是我們尋找解決方案的原因。 – NotDan 2008-12-02 15:48:23