我的问题是继续使用之前的post我将我的组合框放在其他子网格中,名为“grid_SortPart”。所以,我试图设置GetUIElement(“grid_SortPart”)。DataContext = _viewModel;这对我来说不合适。
我也尝试将_target.Loaded更改为_target.LayoutUpdated,但仍然存在同样的问题。在调试过程中我发现,我收到错误“对象引用未设置为对象的实例”。 for object _target。
以下是我通过引用post设置的代码。请告诉我我做错的事情以及解决问题的方法。
public ObservableCollection<ReturnStatus> _status;
[TestInitialize]
public void TestInit()
{
_target = new EfileView();
efvm = new EfileViewModel();
var p1 = new ReturnStatus { Status = "Completed" };
var p2 = new ReturnStatus { Status = "Not Completed" };
_status = new ObservableCollection<ReturnStatus> { p1, p2 };
GetUIElement<Grid>("grid_SortPart").DataContext = efvm;
}
private T GetUIElement<T>(string name) where T : UIElement
{ return (T)_target.FindName(name); }
[Asynchronous]
[TestMethod]
public void TestCurrencySelection()
{
_target.LayoutUpdated += (s, e) =>
{
// Set the currency list explicitly
efvm.ItemSource_ReturnStatus = _status;
var currencyCombo = GetUIElement<ComboBox>("cmb_Returns_2");
// This assert fails as Items.Count == 0
CollectionAssert.AreEquivalent(currencyCombo.Items, _status, "Failed to data-bind currencies.");
EnqueueTestComplete();
};
TestPanel.Children.Add(_target);
}