Hi!
I am trying to unit test some code which manipulates a xamGrid. However, never fills with rows or columns even though I set an ItemsSource on it. This is my unit test. How can I create a XamGrid with rows and columns for testing? By the way, my project is in a WPF context, not Silverlight, but this is the only place with a XamGrid forum.
[TestFixture]public class XamGridUtilsTests { private class DataItem { public int I { get; set; } public string Text { get; set; } public DataItem(int i, string text) { I = i; Text = text; } } [Test] [RequiresSTA] public void SelectAllRows_NoneSelected_SelectsAllRows() { // Arrange ObservableCollection<DataItem> items = new ObservableCollection<DataItem>(); for (int i = 1; i < 4; i++) { items.Add(new DataItem(i, "Column" + i)); } XamGrid grid = new XamGrid(); grid.AutoGenerateColumns = true; grid.ItemsSource = items; CollectionAssert.IsNotEmpty(grid.Rows); <-- THIS FAILS CollectionAssert.IsNotEmpty(grid.Columns); <-- THIS FAILS // Act XamGridUtils.SelectAllRows(grid); // Assert foreach (var row in grid.Rows) { Assert.IsTrue(row.IsSelected); } }}
Hi Atle Rudshau,
as far as I could understand you do not attach the grid to a visual tree. There is huge chance that XamGrid needs to be loaded/measured in order to try to auto-generate its columns and populate the rows.
HTH,
I thought it might be some rendering/visual issue. I am in a nUnit context here and just want a XamGrid with some rows and columns so I can check that my XamGridUtils.SelectAllCels(grid) method actually makes all rows in the grid selected. I don't have nor want any view in my unit-test. I can force some columns by manually adding e.g. TextColumns, but I am still unable to add rows.
I can get columns like this, but how do I add rows?
XamGrid grid = new XamGrid(); grid.AutoGenerateColumns = false; grid.Columns.Add(new TextColumn { Key = "Id" }); grid.Columns.Add(new TextColumn { Key = "Text" });grid.ItemsSource = items;
- Atle