如何在WPF中的datagrid单元格下方显示弹出窗口 [英] How to display a popup below datagrid cell in WPF

查看:171
本文介绍了如何在WPF中的datagrid单元格下方显示弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hai



我想在数据网格的选定单元格下面显示一个弹出窗口。我使用下面的代码,它几乎工作,但问题是当我最大化或调整表格时,位置显示不正确。



请告诉我怎么做这个



谢谢。



在XAML



Popup Margin =0,0,0,0Name =Popup1PlacementTarget ={Binding ElementName = DgvMain}Horizo​​ntalAlignment =LeftVerticalAlignment =TopWidth =250Height =165 >



Popup



我的尝试:



公共静态类DataGridExtensions

{

public static DataGridCell GetCurrentDataGridCell(this DataGrid dataGrid)

{

DataGridCellInfo cellInfo = dataGrid.CurrentCell;

if(cellInfo.IsValid == false)

{

返回null;

}

var cellContent = cellInfo.Column.GetCellConte nt(cellInfo.Item);

if(cellContent == null)

{

返回null;

}

将cellContent.Parent作为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;

}

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;
}

推荐答案

我得到了myseslf

I got it myseslf
     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;
}


这篇关于如何在WPF中的datagrid单元格下方显示弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆