wpf - Find the focused cell from a DataGrid -
i need implement "copy value clipboard" function wpf datagrid should available through common channels: right-click context menu item; menu-key context menu item; ctrl+c hotkey. data grid content comes data binding, since copy command view-only thing, it's implemented in view layer, not viewmodel. don't use icommand event handlers in code behind.
the datagrid's selectionunit set fullrow, arrow navigation keys still work , focus rectangle can seen single cells. single cell can focused while full row selected. command need determine focused cell.
the menu item pretty complicated. click event gives me menuitem instance, can navigate contextmenu , further datagrid. that's all, no clicked-on cell. know how datagrid, there's 1 of them visible @ time.
i need find out cell focused. can't list of selected cells or rows or rows. properties can find point fragments of information, , datagrid.rows doesn't exist. scan entire visual tree focused datagridcell that's not efficient.
any ideas?
later need second function "copy selected rows clipboard" copies values cells in selected rows in format can pasted excel (tab-delimited lines).
a helper function:
/// <summary> /// child element /// </summary> /// <typeparam name="childitem">child item</typeparam> /// <param name="obj">dependency object</param> /// <returns>the child or null</returns> private childitem findvisualchild<childitem>(dependencyobject obj) childitem : dependencyobject { if (obj == null) { return null; } int childcount = visualtreehelper.getchildrencount(obj); (int = 0; < visualtreehelper.getchildrencount(obj); i++) { dependencyobject child = visualtreehelper.getchild(obj, i); if (child != null && child childitem) { return (childitem)child; } else { childitem childofchild = this.findvisualchild<childitem>(child); if (childofchild != null) { return childofchild; } } } return null; }
after datagrid loaded, can call this:
/// <summary> /// cell of datagrid. /// </summary> /// <param name="datagrid">the data grid in question</param> /// <param name="cellinfo">the cell information row of datagrid</param> /// <param name="cellindex">the row index of cell find. </param> /// <returns>the cell or null</returns> private datagridcell trytofindgridcell(datagrid datagrid, datagridcellinfo cellinfo, int cellindex = -1) { datagridrow row; datagridcell result = null; if (cellindex < 0) { row = (datagridrow)datagrid.itemcontainergenerator.containerfromitem(cellinfo.item); } else { row = (datagridrow)datagrid.itemcontainergenerator.containerfromindex(cellindex); } if (row != null) { int columnindex = datagrid.columns.indexof(cellinfo.column); if (columnindex > -1) { datagridcellspresenter presenter = this.findvisualchild<datagridcellspresenter>(row); if (presenter != null) { result = presenter.itemcontainergenerator.containerfromindex(columnindex) datagridcell; } else { result = null; } } } return result; }
then after cell, check isfocused property.
Comments
Post a Comment