wpf datagrid排序时无法执行Up-down操作

发布于 2024-09-11 08:42:57 字数 1107 浏览 2 评论 0原文

嗨,

我有一个数据网格(来自 wpf 工具包),它绑定到一个可观察集合。有两个按钮“向上”和“向下”,可相应移动所选行。 例如:如果数据网格中有 10 个项目,并且如果我选择第 4 个项目并按“向上”箭头,则第 4 个项目将移动到第 3 个项目,而前一个第 3 个项目将是第 4 个项目。即与前一个项目交换。同样,“向下”箭头的方向也相反。

private void BtnUpArrowClick(对象发送者, RoutedEventArgs e) {

  try
  {
    btnDownArrow.IsEnabled = true;
    currentRow = grdSeqData.SelectedIndex;
    if (currentRow == 0)
    {
      btnUpArrow.IsEnabled = false;
    }
    else
    {
      upRow = currentRow - 1;
      if (upRow >= 0)
      {
        sequenceDataList.Move(currentRow, upRow);
        currentRow = upRow;
      }
      else
      {
        btnUpArrow.IsEnabled = false;
      }
    }
  }
  catch (Exception ex)
  {
    CatchExeption(LanguageHandler.GetCurrentCultureText("EventUpArrow_Exception"), ex);
  }
}

当数据网格未排序时,上面的代码可以正常工作。当我通过标题单击对数据网格进行排序,然后执行“向上”或“向下”操作时,数据网格中没有任何反映。

视图正在排序,因为我的绑定集合保持不变。所以我试图将收藏品从视图中移走,但徒劳无功。

ICollectionView 视图 = CollectionViewSource.GetDefaultView(grdSeqData.ItemsSource) as ListCollectionView; view.MoveCurrentToNext();

我认为当应用数据网格排序描述时,视图将不会相应移动。请帮忙。

HI,

I have a datagrid (from wpf toolkit) which is binded to an observable collection . There are two buttons "Up" and "Down" which moves the selected row accordingly.
For eg: If there are 10 items in the datagrid and if I select the 4th item and press "Up" arrow ,then the 4th item will be moved to 3rd item and previous 3rd item will be 4th item. ie swapping with previous item. Similarly "Down" arrow the other way.

private void BtnUpArrowClick(object sender, RoutedEventArgs e)
{

  try
  {
    btnDownArrow.IsEnabled = true;
    currentRow = grdSeqData.SelectedIndex;
    if (currentRow == 0)
    {
      btnUpArrow.IsEnabled = false;
    }
    else
    {
      upRow = currentRow - 1;
      if (upRow >= 0)
      {
        sequenceDataList.Move(currentRow, upRow);
        currentRow = upRow;
      }
      else
      {
        btnUpArrow.IsEnabled = false;
      }
    }
  }
  catch (Exception ex)
  {
    CatchExeption(LanguageHandler.GetCurrentCultureText("EventUpArrow_Exception"), ex);
  }
}

The above code work fine when the datagrid is not sorted. When i sort the datagrid via header click, and then do the "Up" or "Down" operation nothing is reflected in the datagrid.

The view is getting sorted where as my binded collection remains the same. So i tried to move the collection from view but in vain.

ICollectionView view = CollectionViewSource.GetDefaultView(grdSeqData.ItemsSource) as ListCollectionView;
view.MoveCurrentToNext();

I think when the datagrid sortdescription is applied, then the view will not move accordingly. Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文