DevExpress WPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpress WPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。 无论是Office办公软件的衍伸产品,还是以数据为中心的商业智能产品,都能通过DevExpress WPF控件来实现。
用户可以将复选框嵌入到节点中,并允许最终用户选中/取消选中它们:
将复选框嵌入到节点中
1. 将TreeListView.ShowCheckboxes属性设置为true来显示嵌入到节点中的复选框。
2. 设置复选框的值,执行下列之一的操作:
- 指定TreeListView.CheckBoxFieldName属性,将复选框绑定到网格数据源中的布尔字段。
- 检查代码中的节点。
下面的代码示例展示了如何显示复选框并将它们绑定到OnVacation字段:
Xaml
<dxg:GridControl Name="gridControl">
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="Name"/>
<dxg:GridColumn FieldName="Department"/>
<dxg:GridColumn FieldName="Position"/>
</dxg:GridControl.Columns>
<dxg:GridControl.View>
<dxg:TreeListView KeyFieldName="ID" ParentFieldName="ParentID" AutoExpandAllNodes="True"
ShowCheckboxes="True" CheckBoxFieldName="OnVacation" />
</dxg:GridControl.View>
</dxg:GridControl>
C#
public class Employee {
public int ID { get; set; }
public int ParentID { get; set; }
public string Name { get; set; }
public string Position { get; set; }
public string Department { get; set; }
public bool OnVacation { get; set; }
}
VB.NET
Public Class Employee
Public Property ID As Integer
Public Property ParentID As Integer
Public Property Name As String
Public Property Position As String
Public Property Department As String
Public Property OnVacation As Boolean
End Class
注意:在您聚焦另一行之前,TreeListView不会发布被聚焦行的复选框值。要使TreeListView post复选框立即有值,请将TreeListView.ImmediateUpdateCheckBoxState属性设置为true。
检查代码中的节点
使用以下成员在代码中选中/取消选中节点:
- TreeListView.CheckAllNodes:检查所有节点。
- TreeListView.UncheckAllNodes:取消选中所有节点。
- TreeListNode.IsChecked:检查/取消检查节点。
当节点的检查状态改变时,TreeListView会引发TreeListView.NodeCheckStateChanged事件,使用TreeListNode.IsChecked属性来标识节点是否被检查。
不确定状态
将TreeListView.AllowIndeterminateCheckState属性设置为true,允许最终用户将节点的复选框设置为三种状态当中的一个(选中,未选中或不确定):
TIP:在这种情况下,用户可以指定一个复选框的状态,而不管它的父和子的状态。
TreeListNode.IsChecked属性返回以下值:
- true - 如果节点被检查
- false - 如果节点未被检查
- null - 如果节点不确定
递归检查节点
设置TreeListView.AllowRecursiveNodeChecking属性为true,如果您想让控件根据子节点和父节点的选择自动更新检查状态:
- 当您选中/取消选中父节点时,它的子节点也被选中/未选中。
- 当选中/取消选中所有子节点时,它的父节点变为选中/未选中。
- 当您选中/取消选中一个子节点,但并非所有的子节点都被选中/取消选中时,它的父节点将进入不确定的检查状态。
启用状态
使用以下属性将复选框的启用状态绑定到属性:
- TreeListView.IsCheckBoxEnabledFieldName:获取或设置数据源字段的名称,该字段的值决定是否启用节点的复选框。
- TreeListView.IsCheckBoxEnabledBinding:获取或设置确定是否启用节点的复选框绑定。
注意:TreeListView.IsCheckBoxEnabledBinding属性优先于TreeListView.IsCheckBoxEnabledFieldName属性。
下面的代码示例展示了如何将复选框的启用状态绑定到Enabled字段:
Xaml
<dxg:GridControl Name="gridControl">
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="Name"/>
<dxg:GridColumn FieldName="Department"/>
<dxg:GridColumn FieldName="Position"/>
</dxg:GridControl.Columns>
<dxg:GridControl.View>
<dxg:TreeListView KeyFieldName="ID" ParentFieldName="ParentID" AutoExpandAllNodes="True"
ShowCheckboxes="True" CheckBoxFieldName="OnVacation" IsCheckBoxEnabledFieldName="Enabled" />
</dxg:GridControl.View>
</dxg:GridControl>
C#
public class Employee {
public int ID { get; set; }
public int ParentID { get; set; }
public string Name { get; set; }
public string Position { get; set; }
public string Department { get; set; }
public bool OnVacation { get; set; }
public bool Enabled { get; set; }
}
VB.NET
Public Class Employee
Public Property ID As Integer
Public Property ParentID As Integer
Public Property Name As String
Public Property Position As String
Public Property Department As String
Public Property OnVacation As Boolean
Public Property Enabled As Boolean
End Class