0

I'm currently working on expanding the Expander of a grouped DataGrid when an item is selected programatically.

The approach I am going for is to select an object that's in the visual tree related to the selected Item and navigate up for the first Expander it can find, and set the IsExpanded property to true.

I put the pieces together assuming when selecting an item programatically that the SelectedCellsChanged which it does, but unfortunately the e.AddedCells doesn't contain anything. Therefore I'm now unsure about how to get what I need, believing this would be either the cell or row object to search up from in the visual tree.

Any Suggestions / methods are much appreciated,

Thanks.

user8478480
  • 427
  • 3
  • 13
  • 1
    How are you selecting a `DataGrid` item programmatically? When I use `DataGrid.SelectedIndex` it fires `SelectedCellsChanged`. – redcurry Jan 24 '19 at 14:44
  • Sorry mybad, `SelectedCellsChanged` does fire but `e.addedCells` doesn't contain anything. Therefore I can't use it to get to a cell. – user8478480 Jan 24 '19 at 14:48
  • 1
    When I look at `e.AddedCells` I see the one item that I selected programmatically. How are you selecting items programmatically? – redcurry Jan 24 '19 at 14:51
  • I'm selecting it like this for now. `DataGrid.SelectedIndex = x`, for this problem through do note, I am using a custom style that uses Expanders, therefore the rows are not visible when the item is programatically selected. I read something before that it maybe due to virtualisation – user8478480 Jan 24 '19 at 14:53
  • 1
    Can you [turn off virtualization](http://www.ytechie.com/2008/09/disabling-wpf-datagrid-virtualization/) and see if that works? – redcurry Jan 24 '19 at 14:57
  • 1
    Was already on it once i'd written the comment. That now works as I'd have expected originally, Though the method that I was using to get a `DataGridCell` from a `DataGridCellInfo`. The method from here, I think requires the cell to be visible but as it isn't doesnt work now. : https://stackoverflow.com/a/17066695/937093, I'm going to see if I can find another way to grab it, Been a great help though, thanks! – user8478480 Jan 24 '19 at 15:06
  • Can't you ust bind both the datagridrow isselected and your isexpanded to a property IsSelected in your row viewmodel ? – Andy Jan 24 '19 at 17:02
  • @Andy the rows are generated dynamically, so I don't think this is possible? – user8478480 Jan 25 '19 at 09:58
  • There is insufficient information in your post to answer that. You can maybe use a hierarchical datatemplate for your grouping or embed one control inside another or details view... and probably numerous other alternatives. Working with the UI down to data would probably be my last option. – Andy Jan 25 '19 at 11:06

0 Answers0