我已成功实现(并转换为vb)Paul Van Bladel在其博客文章http://blog.pragmaswitch.com/?p=895中的lightswitch数据网格代码中的多选复选框 在博客文章的评论中,Luis Osorio列出了代码(在c#中)来选择和取消全部:
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;
}
};
}
到目前为止,我无法成功转换此代码。以下代码是我到目前为止的代码
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
用法(屏幕后面的代码)
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
此代码编译但SelectAll_Changed方法中存在错误。
&#34;运行命令时发生错误。 错误详情:公共成员&#39; IsChecked&#39;在类型&#39;网格&#39;没找到。&#34;
欢迎任何转换c#的帮助!