当数据网格单元格文本在 wpf 中更改时定位列表视图控件

本文关键字:定位 列表 控件 视图 wpf 数据网 数据 网格 单元格 文本 | 更新日期: 2023-09-27 18:30:52

我有一个数据网格控件,分别是项目代码,项目名称,数量,费率,金额。我想在数据网格中"项目名称"列的单元格上键入内容时填充列表视图控件上的项。列表视图必须出现在我键入的单元格的下方。我不知道当单元格文本更改时如何获得位置列表视图.帮助我摆脱这些.

我的窗口看起来像:

 <Grid  Width="900" Height="567" Background="{StaticResource Background}">
      <my:DataGrid Name="dgvSales" BeginningEdit="dgvSales_BeginningEdit" AutoGenerateColumns="False" Margin="13,150,9,117" KeyDown="dgvSales_KeyDown" MouseDown="dgvSales_MouseDown" PreparingCellForEdit="dgvSales_PreparingCellForEdit" MouseRightButtonUp="dgvSales_MouseRightButtonUp">
                    <my:DataGrid.Columns>
                        <my:DataGridTextColumn x:Name="naam" Header="Code" Width="120" Binding="{Binding ProductCode}"></my:DataGridTextColumn>
                        <my:DataGridTextColumn Header="Product Name" Width="200" Binding="{Binding ProductName}"></my:DataGridTextColumn>
                        <my:DataGridComboBoxColumn Header="Unit" Width="100"></my:DataGridComboBoxColumn>
                        <my:DataGridTextColumn Header="Purchase Rate" Width="100" Binding="{Binding PurchaseRate}"></my:DataGridTextColumn>
                        <my:DataGridTextColumn Header="Qty" Width="100" Binding="{Binding Qty}"></my:DataGridTextColumn>
                        <my:DataGridTextColumn Header="Amount" Width="100" Binding="{Binding Amount}"></my:DataGridTextColumn>
                    </my:DataGrid.Columns>
                </my:DataGrid>
<ListView Height="161" ItemsSource="{Binding}" Margin="461,0,51,257" Name="lstvwProductCode" VerticalAlignment="Bottom" Visibility="Collapsed" Width="388">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn DisplayMemberBinding="{Binding Path=Record_Id}" Header="Record ID" Width="0" />
                            <GridViewColumn DisplayMemberBinding="{Binding Path=Product_Code}" Header="Pcode" Width="110" />
                            <GridViewColumn DisplayMemberBinding="{Binding Path=Product_Name}" Header="Product" Width="180" />
                        </GridView>
                    </ListView.View>
                </ListView>
</Grid>

当数据网格单元格文本在 wpf 中更改时定位列表视图控件

你得到你的ListViewItem的位置,如下所示:

FrameworkElement item = (FrameworkElement)DataGrid.ItemContainerGenerator.ContainerFromItem(data);
Point position = item.TranslatePoint(new Point(0, item.ActualHeight), Window.GetWindow(DataGrid));

现在,您可以更改列表视图的位置:

ListView.RenderTransform = new TranslateTransform(position.X, position.Y);

编辑:您还可以使用 BeginningEdit-Event 的 EventArgs:

    private void DataGrid_OnBeginningEdit(object sender, DataGridBeginningEditEventArgs e)
    {
        Point newPosition = new Point();
        if (!(lstvwProductCode.RenderTransform is TransformGroup))
            lstvwProductCode.RenderTransform = new TransformGroup();// set the ListView's RenderTransform to a TransformGroup if it isn't already
        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)
        {
            double 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(rowX + 5, e.Row.ActualHeight), this); //translate this point to a point on your main window.
        }
        if (newPosition != new Point())
        {
            Point tPoint = lstvwProductCode.TranslatePoint(new Point(0, 0), this);//add this point
            ((TransformGroup) lstvwProductCode.RenderTransform).Children.Add(
                new TranslateTransform(newPosition.X - tPoint.X, newPosition.Y - tPoint.Y));
        }
    }
    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;
    }