Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai

I Want to display a popup below the selected cell of datagrid. I use the code below ,it is working almost but the problem is when I maximize or resize the form the position is displayed not correctly.

Please tell me how to do this

Thanks.

In XAML

Popup Margin="0,0,0,0" Name="Popup1" PlacementTarget="{Binding ElementName=DgvMain}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="250" Height="165" >

Popup

What I have tried:

public static class DataGridExtensions
{
public static DataGridCell GetCurrentDataGridCell(this DataGrid dataGrid)
{
DataGridCellInfo cellInfo = dataGrid.CurrentCell;
if (cellInfo.IsValid == false)
{
return null;
}
var cellContent = cellInfo.Column.GetCellContent(cellInfo.Item);
if (cellContent == null)
{
return null;
}
return cellContent.Parent as DataGridCell;
}
}


private void DgvMain_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
DataGridCell Cell = DgvMain.GetCurrentDataGridCell();
var Position = Cell.PointToScreen(new Point(0, 0));
// MessageBox.Show("X=" + Position.X.ToString() + ", Y=" + Position.Y.ToString(), "Position");

Popup1.PlacementRectangle = new Rect(Position.X - DgvMain.Margin.Left - 110 , Position.Y- DgvMain.Margin.Top-115, 0, 0);
Popup1.IsOpen = true;
}
Posted
Updated 17-Oct-16 20:44pm
v8

1 solution

I got it myseslf
C#
     private double GetColumnXPosition(DataGridColumn column, DataGrid grid)
{
     double result = 0.0;

    if (grid == null)
        return result;

    for (int i = 0; i < grid.Columns.Count; i++)
    {
        DataGridColumn dgc = grid.Columns[i];
        if (dgc.Equals(column))
            break;

        result += dgc.ActualWidth;
    }
    return result;
}
private DataGrid GetRowsDataGrid(DataGridRow row)
{
    DependencyObject result = VisualTreeHelper.GetParent(row);
    while (result != null && !(result is DataGrid))
    {
        result = VisualTreeHelper.GetParent(result);
    }
    return result as DataGrid;
}

private void DgvMain_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
    Point newPosition = new Point();

    double rowX=0;
    DataGrid dg = GetRowsDataGrid(e.Row); //get the Row's corresponding DataGrid with the help of the VisualTreeHelper. You cant use the Column here, because it won't get added to the visual tree.
    if (dg != null)
    {
        rowX = GetColumnXPosition(e.Column, dg); //get the x position. Here you can't use .TranslatePoint because - again - it doesn't belong to the visual tree, so you have to sum up all columns width' to the column where the changes are made.
        newPosition = e.Row.TranslatePoint(new Point( DgvMain.RowHeaderWidth, e.Row.ActualHeight), this); //translate this point to a point on your main window.
    }

    if (newPosition != new Point())
    {

              Popup1.PlacementRectangle = new Rect(rowX + DgvMain.RowHeaderActualWidth, newPosition.Y - DgvMain.Margin.Top, 0, 0);

    }

    Popup1.IsOpen = true;

}


private void DgvMain_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    Popup1.IsOpen = false;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900