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
Hello Atle,
I can suggest you try to call XamGrid's Measure and Arrabge methods in order to force the XamGrid to calculate its dimesions.
We are still following this forum thread.
Please feel free to let us know if you have any other questions with this matter.
Our developers said that in order to do your tests the XamGRid should be in the visual tree. They also said that you can try to invoke the XamGrid's internal method LoadData() via reflection, but this could only be done in WPF.
Hope this helps you.
Did not work. I guess since I am not in a GUI context here it won't repaint. I tried this:
XamGrid grid = new XamGrid(); grid.AutoGenerateColumns = false; grid.Columns.Add(new TextColumn { Key = "Id" }); grid.Columns.Add(new TextColumn { Key = "Text" }); grid.ItemsSource = items; grid.Measure(new Size(100.0, 100.0)); grid.Arrange(new Rect(0.0, 0.0, 100.0, 100.0)); grid.InvalidateMeasure(); grid.InvalidateArrange(); grid.InvalidateVisual();grid.InvalidateData();
Atle