我有一个Datagrid控件有5列,分别是Item Code,Item Name,Qty,Rate,Amount。当我在Datagrid中的Item Name列上的单元格上键入内容时,我想在listview控件上填充Items。列表视图必须出现在我键入的单元格的下方。我不知道如何在单元格文本更改时将列表视图放到列表视图中。帮助我摆脱这些。
我的窗口如下:
<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>
答案 0 :(得分:0)
你得到了ListViewItem的意思:
FrameworkElement item = (FrameworkElement)DataGrid.ItemContainerGenerator.ContainerFromItem(data);
Point position = item.TranslatePoint(new Point(0, item.ActualHeight), Window.GetWindow(DataGrid));
现在您可以更改ListView的位置:
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;
}