Empty Popup window for datagridcell
I'm using a Popup (System.Windows.FrameworkElement.Popup) to display a note to the user if e.g. he exceeds the maximum string length in a text box.
I'm using the PlacementTarget property to assign a frameworkElement to make sure that the Popup is displayed near the corresponding control and not near to the mouse pointer.
Everything is working fine for a normal textBox, but using a popup in 开发者_JS百科a datagrid poses a problem.
Using the popup with PlacementTarget = theDataGrid is working but the popup is displayed at the left upper corner of the datagrid (with Placement = PlacementMode.Left) but I want to display the popup near the cell that triggers the display of the popup.
Now I am using the following code to determine the corresponding DataGridCell:
static DataGridCell TryToFindGridCell(DataGrid grid, DataGridCellInfo cellInfo)
{
DataGridCell result = null;
DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(cellInfo.Item);
if (row != null)
{
int columnIndex = grid.Columns.IndexOf(cellInfo.Column);
if (columnIndex > -1)
{
DataGridCellsPresenter presenter = VisualTreeHelperMethods.GetVisualChild<DataGridCellsPresenter>(row);
result = presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex) as DataGridCell;
}
}
return result;
}
which seems to work fine, but using the DataGridCell (which is a FrameworkElement in contrast to DataGridCellInfo) has the effects, that
the popup is displayed at the correct location, near the DataGridCell
but is empty
Can anybody help or explain why the popup is empty?
For now I stick to displaying the popup with PlacementTarget = theDataGrid which is however a bit unsatisfactory.
精彩评论