我有这样一个DataGrid
_productsPosition = new ObservableCollectionEx<NotifiedPositionInfo>();
_itemSourceList = new CollectionViewSource() { Source = _productsPosition };
_itemSourceList.Filter += new FilterEventHandler(FilteringProduct);
PositionsGrid.ItemsSource = _itemSourceList.View;
当我尝试拖动这个DataGrid行,我能获得行上的CollectionView的索引。 但这个指标不能帮我在收集_productsPosition相同的元素,因为的CollectionView被过滤器修改。
因此,我有我如何才能继续前进了的CollectionView元素,但不是_productsPosition的问题。 像这样的事情会抛出异常:
NotifiedPositionInfo prev_position = PositionsGrid.ItemContainerGenerator.Items[_prevRowIndex] as NotifiedPositionInfo;
PositionsGrid.Items.RemoveAt(_prevRowIndex);
PositionsGrid.Items.Insert(index, prev_position);
异常的内容是:
未处理的异常:System.InvalidOperationException:操作是无效的,同时的ItemsSource正在使用中。 访问和修改元素与ItemsControl.ItemsSource代替。
在System.Windows.Controls.ItemCollection.CheckIsUsingInnerView()
在System.Windows.Controls.ItemCollection.RemoveAt(的Int32 removeIndex)
工作示例:
<DataGrid Name="PositionsGrid" Background="AliceBlue" AutoGenerateColumns="False" CanUserAddRows="False" ItemsSource="{Binding}"
AllowDrop="True" GridLinesVisibility="Horizontal" HorizontalGridLinesBrush="#FFDEDEDE">
<DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF0096FD"/>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Product Name" Width="130" IsReadOnly="True" Binding="{Binding ProductName}"/>
<DataGridTextColumn Header="Positions" Width="*" IsReadOnly="True" Binding="{Binding Position}"/>
</DataGrid.Columns>
</DataGrid>
拖放功能:
PositionsGrid.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(positionsGrid_PreviewMouseLeftButtonDown);
PositionsGrid.Drop += new DragEventHandler(positionsGrid_Drop);
private bool isTheMouseOnTargetRow(Visual target, GetDragDropPosition pos)
{
try
{
Rect posBounds = VisualTreeHelper.GetDescendantBounds(target);
Point theMousePos = pos((IInputElement)target);
return posBounds.Contains(theMousePos);
}
catch (Exception)
{
return false;
}
}
private DataGridRow getDataGridRowItem(int index)
{
if (PositionsGrid.ItemContainerGenerator.Status != System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
return null;
return PositionsGrid.ItemContainerGenerator.ContainerFromIndex(index) as DataGridRow;
}
private int getDataGridItemCurrentRowIndex(GetDragDropPosition pos)
{
int curIndex = -1;
for (int i = 0; i < PositionsGrid.Items.Count; i++)
{
DataGridRow item = getDataGridRowItem(i);
if (isTheMouseOnTargetRow(item, pos))
{
curIndex = i;
break;
}
}
return curIndex;
}
private int _prevRowIndex = -1;
private void positionsGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_prevRowIndex = getDataGridItemCurrentRowIndex(e.GetPosition);
if (_prevRowIndex < 0)
return;
PositionsGrid.SelectedIndex = _prevRowIndex;
NotifiedPositionInfo selected_positionInfo = PositionsGrid.Items[_prevRowIndex] as NotifiedPositionInfo;
if (selected_positionInfo == null)
return;
DragDropEffects dragdrop_effects = DragDropEffects.Move;
if (DragDrop.DoDragDrop(PositionsGrid, selected_positionInfo, dragdrop_effects) != DragDropEffects.None)
PositionsGrid.SelectedItem = selected_positionInfo;
}
private void positionsGrid_Drop(object sender, DragEventArgs e)
{
if (_prevRowIndex < 0)
return;
int index = this.getDataGridItemCurrentRowIndex(e.GetPosition);
if (index < 0)
return;
if (index == _prevRowIndex)
return;
if (index == PositionsGrid.Items.Count - 1)
{
MessageBox.Show("This row-index cannot be used for Drop Operations");
return;
}
NotifiedPositionInfo prev_position = PositionsGrid.ItemContainerGenerator.Items[_prevRowIndex] as NotifiedPositionInfo;
PositionsGrid.Items.RemoveAt(_prevRowIndex);
PositionsGrid.Items.Insert(index, prev_position);
}