【问题标题】:Dynamically hide columns of a nested datagrid动态隐藏嵌套数据网格的列
【发布时间】:2014-05-03 10:59:15
【问题描述】:

我有一个嵌套在数据网格中的数据网格。只有在原始数据网格中按下切换按钮时,嵌套数据网格才可见。

这很好用,但是我希望能够隐藏嵌套数据网格中的一些列。

我正在使用 MVVM。

我尝试通过将 Column 的 Visibility 属性绑定到我的视图模型中的可见性属性来实现这一点,我还尝试将宽度绑定到我的视图模型中的整数属性,但没有成功。

我已经尝试通过后面的代码来实现这一点,但是我无法访问嵌套的数据网格 - 它似乎根本不在 VisualTree 中。

我试过的代码是:

  Private Sub DgRowLoading(sender As Object, e As DataGridRowEventArgs) Handles OriginalDataGridName.LoadingRow

     Dim test As DataGrid = OriginalDataGridName.FindName("NestedDataGridName")

End Sub

我什至尝试在加载嵌套数据网格但未找到嵌套数据网格后循环遍历原始数据网格的可视化树。我使用的代码是:

 Private Sub LoopThroughControls(parent As UIElement)
    Dim count As Integer = VisualTreeHelper.GetChildrenCount(parent)
    If count > 0 Then
        For i As Integer = 0 To count - 1
            Dim child As UIElement = DirectCast(VisualTreeHelper.GetChild(parent, i), UIElement)
            Dim childTypeName As String = child.[GetType]().ToString()
            LoopThroughControls(child)
        Next
    End If
End Sub

【问题讨论】:

  • 如果您知道 Datagrids 的名称,为什么要这么复杂,只需将这个 NestedDataGridName.Columns[0].Visibility = Visibility.Collapsed;
  • 因为您无法通过名称访问嵌套控件

标签: vb.net silverlight xaml mvvm


【解决方案1】:

几天前我也完成了类似的事情,但我没有嵌套数据网格。我正在处理单个数据网格,this question 帮助我完成了任务。

【讨论】:

  • 谢谢,但问题在于嵌套数据网格,我可以在普通数据网格上轻松隐藏列。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-28
  • 1970-01-01
  • 2023-01-17
相关资源
最近更新 更多