Silverlight DataGrid 导航和隐藏行

发布于 2024-08-31 13:29:40 字数 756 浏览 0 评论 0原文

我有一个场景,我想在 Silverlight 3 的 DataGrid 中显示分层数据,同时标题行仍然具有标准的单元格集并且可编辑。用户需要能够折叠和展开行以隐藏子行。

我通过在每个父行上放置一个按钮来实现此目的,该按钮看起来像是折叠或展开字形,具体取决于其状态。单击此按钮可以操作其子行的数据项的属性。每行的可见性都绑定到数据项的此属性。

这在某种程度上工作得不错,尽管它确实存在性能问题,因为网格渲染的列比它需要的多。

我现在的问题是,当用户向上或向下敲击键盘时,他们可以导航到隐藏的行。

例如,如果我有一个像

1 Parent (Expanded, Visible) 这样

的结构

1a(可见)

1b(可见)

2 父级(折叠、可见)

2a(隐藏)

3 父级(展开、可见)

3a(可见)

如果我选择了 [2 Parent] 并且按下键盘上的向下箭头,我希望选择转到 [3 Parent],但它会转到 [2a],即使 [2a] 的行可见性设置为 Visibility.Collapsed。

我希望能够拦截键盘事件(通过类似不存在的 PreviewKeyDown 事件之类的事件)并自己处理它,或者找到某种方法来欺骗 DataGrid 的内部移动到正确的项目。

此时,我相当投入地调整行可见性以隐藏这些项目。

有什么想法吗?

I have a scenario where I want to show hierarchical data in a DataGrid in Silverlight 3 while having the header rows still have the standard set of cells and being editable. The user needs to be able to collapse and expand rows to hide child rows.

I've accomplished this by having a button on each parent row that looks like either a collapse or an expand glyph depending on its state. Clicking this manipulates a property on the data item for its child rows. Each row has its visibility bound to this property of the data item.

This works somewhat decently, though it does have performance problems with the grid rendering more columns than it needs to.

My problem now is that when the user hits up or down on the keyboard they are able to navigate to hidden rows.

For example if I have a structure like

1 Parent (Expanded, Visible)

1a (Visible)

1b (Visible)

2 Parent (Collapsed, Visible)

2a (Hidden)

3 Parent (Expanded, Visible)

3a (Visible)

If I have [2 Parent] selected and I hit the down arrow on the keyboard I would expect the selection to go to [3 Parent] but it goes to [2a] instead even though [2a]'s row visibility is set to Visibility.Collapsed.

I'd like to be able to either intercept the keyboard event (via something like the non-existent PreviewKeyDown event) and handle it myself or to find some way of tricking the DataGrid's internals of moving to the correct item.

At this point I am fairly invested in tweaking the row visibility for hiding these items.

Any ideas?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

姜生凉生 2024-09-07 13:29:40

好吧,我发帖后就在想这个问题。我曾尝试使用 PagedCollectionView 进行分组,但对组样式不满意,但我没有尝试使用它进行过滤。

我现在将 DataGrid 的旧 ItemsSource 包装在 PagedCollectionView 内,然后将其 Filter 设置为基于数据项的可见性属性返回的方法。

  Private Sub UpdateFilter(ByVal view As PagedCollectionView)
    If view IsNot Nothing Then
      view.Filter = Nothing
      view.Filter = New Predicate(Of Object)(AddressOf FilterRows)
    End If
  End Sub

  Private Function FilterRows(ByVal obj As Object) As Boolean
    Dim item As MyDataItem = obj
    Return item.IsVisible = Windows.Visibility.Visible
  End Function

这种方法的唯一问题是,由于它不使用绑定来过滤,因此如果它在 MyDataItem.IsVisible 上看到属性更改事件,则不会刷新过滤器。

为了解决这个问题,我响应行项目上的事件并调用 UpdateFilter项目展开或折叠的时间。我将 View.Filter 清空,因为如果不这样做,DataGrid 将不会更新其过滤行集。

这也解决了我涉及折叠/展开的性能问题,因为 DataGrid 可以更好地处理过滤的行,因为它不必实例化一行只是为了将其可见性设置为折叠。

希望这可以帮助其他遇到类似困难的人。

Okay, I was thinking about this after I posted it. I had tried using PagedCollectionView for grouping and wasn't happy with the group styles but I hadn't tried using it for filtering.

I'm now wrapping my DataGrid's old ItemsSource inside of a PagedCollectionView and then setting the Filter of it to a method that returns based on the visibility property of my data item.

  Private Sub UpdateFilter(ByVal view As PagedCollectionView)
    If view IsNot Nothing Then
      view.Filter = Nothing
      view.Filter = New Predicate(Of Object)(AddressOf FilterRows)
    End If
  End Sub

  Private Function FilterRows(ByVal obj As Object) As Boolean
    Dim item As MyDataItem = obj
    Return item.IsVisible = Windows.Visibility.Visible
  End Function

The only problem with this approach is that since it's not using binding to filter it will not refresh the filter if it sees a property changed event on MyDataItem.IsVisible

To solve this, I'm responding to events on my row items and calling UpdateFilter every time an item is expanded or collapsed. I'm nulling out View.Filter because if I don't then the DataGrid won't update its set of filtered rows.

This also solves my performance woes involving collapsing / expanding since the DataGrid handles filtered rows much better since it doesn't have to instantiate a row just to have its visibility set to Collapsed.

Hope this helps anyone else out there with similar difficulties.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文