如何在PreparingCellForEdit Silverlight Datagrid中获取隐藏列控制值

时间:2013-08-04 05:48:50

标签: silverlight datagrid silverlight-4.0

如何在PreparingCellForEdit Silverlight Datagrid中获取隐藏列控件值

代码如下:

Private Sub TaskDataGrid_LoadingRow(ByVal sender As System.Object, ByVal e As 
System.Windows.Controls.DataGridRowEventArgs)

    Dim row As DataGridRow = e.Row 
    Dim cellContent As FrameworkElement = TaskDataGrid.Columns(8).GetCellContent(e.Row)

    Dim cboLabValidated As ComboBox = CType(cellContent.FindName("cboLabValidated"), ComboBox)
    Dim ViewModel As New NonFirmWareNewRequestViewModel()
    If cboLabValidated IsNot Nothing Then
        cboLabValidated.ItemsSource = ViewModel.YesNoValues
    End If 
    TaskDataGrid.Columns(1).Visibility = Visibility.Collapsed
End Sub

在上面的代码中我隐藏了LoadingRow Event中的第1列,需要在PreparingCellForEdit中获取该列的值

准备CellForEdit的代码如下:

Dim fe As FrameworkElement = TaskDataGrid.Columns(5).GetCellContent(e.Row)
                    Dim fe1 As FrameworkElement = TaskDataGrid.Columns(1).GetCellContent(e.Row)

                    Dim gridCmbo As Grid = DirectCast(fe, Grid)

                    Dim gridCmbo1 As Grid = DirectCast(fe1, Grid)

                    Dim lbltaskId As Label = CType(gridCmbo1.FindName("lbltaskId"), Label)

                    Dim cboCompVerSel As ComboBox = CType(gridCmbo.FindName("cboCompVerSel"), ComboBox)

                    Dim lblCompVer As Label = CType(gridCmbo.FindName("lblCompVer"), Label)

我正在使用标签控件来显示第1列,我正在识别标签控件对象,但内容变空了。

1 个答案:

答案 0 :(得分:0)

这很好用

1)在加载行事件中我们不应该隐藏列,如果你隐藏列,你就无法从列中获取值,让数据在LoadingRow事件中首先加载。

2)隐藏Selection changed事件代码中的列,如下所示:

 Private Sub TaskDataGrid_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles TaskDataGrid.SelectionChanged
        TaskDataGrid.Columns(1).Visibility = Visibility.Collapsed 
    End Sub

此事件将在LoadingRow Datagrid事件或PreparingCellForEdit事件之后触发,一旦Row加载,我们可以隐藏列并可以获取您想要的值。如果您在LoadingRow事件中隐藏而不加载数据则无法获取值在Datagrid模板或数据列中控制。