Multi selection checkbox - select/unselect all in Lightswitch datagrid

94 views Asked by At

I have succeeded in implementing (and converting to vb), Paul Van Bladel's multi selection checkbox in a lightswitch datagrid code from his blog post http://blog.pragmaswitch.com/?p=895 In the comments of the blog post, Luis Osorio listed code (in c#) to select and unselect all:

        Public Static Class DependencyObjectExtensions
{
Public Static IEnumerable GetVisualChildren(this DependencyObject depObj)
where T :  DependencyObject
{
If (depObj == null) yield break;

For (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);

var t = child As T;
If (t!= null) yield Return t;

foreach (var item in GetVisualChildren(child))
{
yield return item;
}
}
}
}

now in the screen codebehind
Boolean SelectAll;

Partial void SelectAll_Changed()
{

this.FindControl(“MyGrid”).ControlAvailable += (sender, eventArg) =>
{
DataGrid Grid = (DataGrid)eventArg.Control;

foreach (var Check in Grid.GetVisualChildren())
{
Check.IsChecked = SelectAll;
}
};
}

I have so far been unable to convert this code successfully. The following code is what I have so far

Option Infer On
Imports System.Collections
Imports System.Runtime.CompilerServices
Namespace LightSwitchApplication
Public Module DependencyObjectExtensions
    <Extension()>
    Public Iterator Function GetVisualChildren(ByVal depObj As DependencyObject) As IEnumerable
        If depObj Is Nothing Then
            Return
        End If

        Dim i As Integer = 0
        Do While i < VisualTreeHelper.GetChildrenCount(depObj)
            Dim child = VisualTreeHelper.GetChild(depObj, i)

            Dim a = TryCast(child, DependencyObject)
            If a IsNot Nothing Then
                Yield a
            End If

            For Each item In GetVisualChildren(child)
                Yield item
            Next item
            i += 1
        Loop
    End Function
End Module
End Namespace

Usage (code behind the screen)

    Dim SelAll As Boolean

    Private Sub AllSelect_Execute()
        If SelAll = False Then
            SelAll = True
        Else
            SelAll = False
        End If
        SelectAll_Changed()
    End Sub

    Private Sub SelectAll_Changed()
        AddHandler Me.FindControl("QuoteDetails").ControlAvailable, Sub(sender, eventArg)
                                                                        Dim Grid As DataGrid = CType(eventArg.Control, DataGrid)

                                                                        For Each Check In Grid.GetVisualChildren()
                                                                            Check.IsChecked = SelAll
                                                                        Next Check
                                                                    End Sub
    End Sub

This code compiles but errors in the SelectAll_Changed method.

"An error occurred while running the command. Error details: Public member 'IsChecked' on type 'Grid' not found."

Any help to convert the c# is welcome!

0

There are 0 answers