Hi,
I'm unable to programmatically set cell focus on a XamPivotGrid (v12.1). I'm using a custom XamPivotGrid object deriving from XamPivotGrid, within this class I have a SetFocus() method which should programmatically set the focus on a cell. Here is a code snippet of where I got stuck:
public void SetFocus(object cellRecord){ bool success = this.Focus(); foreach (PivotDataRow row in this.GridLayout.Rows) { foreach (PivotCell cell in row.Cells) { ICell c = cell.Data as ICell; if (c != null) { List<object> records = GetRecords(c); foreach (object record in records) { if (record == cellRecord) { // bool s = this.SelectCell(cell, PivotInvokeAction.Click); // Note: this doesn't work, always returns false // TODO: set focus on cell here return; } } } } }}private List<object> GetRecords(ICell cell){ List<object> records = new List<object>(); if (cell != null) { FlatDataSource flatDataSource = this.DataSource as FlatDataSource; if (flatDataSource != null) { foreach (int index in flatDataSource.GetCellItemsIndexes(cell)) { records.Add(flatDataSource.GetRecord(index)); } } } return records;}
When I run this code I get to the TODO comment (so I find the correct cell), but I'm not able to set the focus on the cell. I've tried SelectCell, SetActiveCell and setting IsActive and IsSelected to true. SelectCell and SetActiveCell always return false, setting the "Is" flags has no visual effect.
Can you please have a look and let me know what I'm missing?
Regards,
Roman
After some further analysis, I think it would be most useful if you can show me a working example where you have UI focus on a cell, then do a flatdatasource refresh by setting the ItemSource to null and then setting it to the original observable collection, and then setting UI focus on the cell which was focused before the refresh.
I can't get this to work :(
no worries, I found a way to do it: I subscribe to the ResultChanged event of the FlatDataSource and after refreshing the ItemsSource, this event will fire twice. On the second event fire, I subscribe to the XamPivotGrid's LayoutUpdated event and then call SetActiveCell() on that event.
This does the job, but it looks like quite a hacky way of doing it (especially cause the ResultChanged fires twice and one can only do it on the second call), so if you know a more elegant version or if there is some kind of logic that makes this work out of the box please share it with us.