我有一个嵌套在数据网格中的数据网格。只有在原始数据网格中按下切换按钮时,嵌套数据网格才可见。
这很好用,但是我希望能够隐藏嵌套数据网格中的一些列。
我正在使用MVVM。
我尝试通过将Column的Visibility属性绑定到视图模型中的visibility属性来实现此目的,我也尝试将宽度绑定到viewmodel中的整数属性而没有运气。
我尝试通过后面的代码实现这一点,但是我无法访问嵌套的数据网格 - 它似乎根本就不在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