我想用datagrid
单元格创建一个popup
。我在windows窗体中使用下面的代码,它运行正常。
但我希望它将其转换为用户控件。请帮助如何做到这一点。
XAML
private void Dgv_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
Point newPosition = new Point();
double rowX = 0;
DataGrid dg = GetRowsDataGrid(e.Row);
if (dg != null)
{
rowX = GetColumnXPosition(e.Column, dg);
newPosition = e.Row.TranslatePoint(new Point(Dgv.RowHeaderWidth, e.Row.ActualHeight), this);
}
if (newPosition != new Point())
{
Popup1.PlacementRectangle = new Rect(rowX + Dgv.RowHeaderActualWidth, newPosition.Y - Dgv.Margin.Top, 0, 0);
}
Popup1.IsOpen = true;
}
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 Dgv_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
Popup1.IsOpen = false;
}
我想使用下面的代码将其转换为用户控件 XAML
<DataGrid x:Class="WpfGrid.WGrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfGrid"
mc:Ignorable="d"
d:DesignHeight="300" Width="500" PreviewKeyDown="DataGrid_PreviewKeyDown" BeginningEdit="DataGrid_BeginningEdit" SelectionMode="Single" SelectionUnit="Cell" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" CanUserResizeRows="False" CanUserSortColumns="False" CanUserResizeColumns="False" EnableRowVirtualization="False" PreviewTextInput="DataGrid_PreviewTextInput">
<DataGrid.Resources >
<Popup x:Key="GPopup" Margin="0,0,0,0" Name="XPopup" PopupAnimation="Slide" PlacementTarget="{Binding ElementName=WGrid}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="250" Height="150" />
</DataGrid.Resources>
代码
private void DataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
Point newPosition = new Point();
double rowX = 0;
DataGrid dg = GetRowsDataGrid(e.Row);
if (dg != null)
{
rowX = GetColumnXPosition(e.Column, dg);
newPosition = e.Row.TranslatePoint(new Point(Dgv.RowHeaderWidth, e.Row.ActualHeight), this);
}
if (newPosition != new Point())
{
**//Upto this it is ok here I want to access the <DataGrid.Resources > Gpopup and show it.**
Popup1.PlacementRectangle = new Rect(rowX + Dgv.RowHeaderActualWidth, newPosition.Y - Dgv.Margin.Top, 0, 0);
}
Popup1.IsOpen = true;
}
我不知道天气我是对的,因为我是Wpf的新手所以请帮助如何实现这一点,或者是否有任何替代创建用户控制 在单元格下方弹出窗口。 感谢