public void GridRowNumberConverter_Convert_WhenInputNotDataGridRow_ReturnsMinus1() { //------------Setup for test-------------------------- var converter = new GridRowNumberConverter(); DataGrid dataGrid = new DataGrid { AutoGenerateColumns = true }; var itemsSource = new List<string> { "Item 1 ", "Item 2" }; dataGrid.ItemsSource = itemsSource; dataGrid.SelectedItem = itemsSource[0]; var row = new Object(); //------------Execute Test--------------------------- var result = converter.Convert(row, typeof(string), null, CultureInfo.CurrentCulture); //------------Assert Results------------------------- Assert.AreEqual(-1, result); }
public void GridRowNumberConverter_Convert_WhenValidRow_ReturnsRowIndex() { var converter = new GridRowNumberConverter(); DataGrid dataGrid = new DataGrid { AutoGenerateColumns = true }; var itemsSource = new List<string> { "Item 1 ", "Item 2" }; dataGrid.ItemsSource = itemsSource; dataGrid.SelectedItem = itemsSource[0]; IItemContainerGenerator generator = dataGrid.ItemContainerGenerator; var position = generator.GeneratorPositionFromIndex(0); using(generator.StartAt(position, GeneratorDirection.Forward, true)) { foreach(object o in dataGrid.Items) { DependencyObject dp = generator.GenerateNext(); generator.PrepareItemContainer(dp); } } var row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.SelectedItem); //------------Execute Test--------------------------- var result = converter.Convert(row, typeof(string), null, CultureInfo.CurrentCulture); //------------Assert Results------------------------- Assert.AreEqual(1, result); }