界面组件DevExpress WPF中文教程:Grid - 如何检查节点?

DevExpress WPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpress WPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。 无论是Office办公软件的衍伸产品,还是以数据为中心的商业智能产品,都能通过DevExpress WPF控件来实现。

获取DevExpress WPF v25.1正式版下载

用户可以将复选框嵌入到节点中,并允许最终用户选中/取消选中它们:

将复选框嵌入到节点中
  1. TreeListView.ShowCheckboxes属性设置为true来显示嵌入到节点中的复选框。

  2. 设置复选框的值,执行下列之一的操作:

下面的代码示例展示了如何显示复选框并将它们绑定到OnVacation字段:

Xaml

XML 复制代码
<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#

cs 复制代码
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

vbnet 复制代码
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会引发TreeListView.NodeCheckStateChanged事件,使用TreeListNode.IsChecked属性来标识节点是否被检查。

不确定状态

TreeListView.AllowIndeterminateCheckState属性设置为true,允许最终用户将节点的复选框设置为三种状态当中的一个(选中,未选中或不确定):

TIP:在这种情况下,用户可以指定一个复选框的状态,而不管它的父和子的状态。

TreeListNode.IsChecked属性返回以下值:

  • true - 如果节点被检查
  • false - 如果节点未被检查
  • null - 如果节点不确定
递归检查节点

设置TreeListView.AllowRecursiveNodeChecking属性为true,如果您想让控件根据子节点和父节点的选择自动更新检查状态:

  • 当您选中/取消选中父节点时,它的子节点也被选中/未选中。
  • 当选中/取消选中所有子节点时,它的父节点变为选中/未选中。
  • 当您选中/取消选中一个子节点,但并非所有的子节点都被选中/取消选中时,它的父节点将进入不确定的检查状态。
启用状态

使用以下属性将复选框的启用状态绑定到属性:

注意TreeListView.IsCheckBoxEnabledBinding属性优先于TreeListView.IsCheckBoxEnabledFieldName属性。

下面的代码示例展示了如何将复选框的启用状态绑定到Enabled字段:

Xaml

XML 复制代码
<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#

cs 复制代码
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

vbnet 复制代码
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

更多DevExpress线上公开课、中文教程资讯请上中文网获取

相关推荐
紫丁香5 分钟前
Selenium自动化测试详解1
python·selenium·测试工具·ui
GISer_Jing18 分钟前
前端组件库——shadcn/ui:轻量、自由、可拥有,解锁前端组件库的AI时代未来
前端·人工智能·ui
唐青枫20 分钟前
C#.NET SignalR + Redis Backplane 深入解析:多节点部署与跨实例消息同步
c#·.net
Java开发追求者1 小时前
.NET Framework,Version=v4.8下载地址
.net·.net framework·version=v4.8
毕设源码-赖学姐2 小时前
【开题答辩全过程】以 基于.NET MVC的婚庆服务系统设计为例,包含答辩的问题和答案
mvc·.net
步步为营DotNet2 小时前
#.NET Aspire在云原生应用部署与管理中的深度实践
云原生·.net
bugcome_com2 小时前
WPF 命令 ICommand 从原理到实战
后端·wpf·icommand
rjc_lihui3 小时前
IntelliSense: 无法打开 源 文件 “ui_mainwindow.h“ demo\qtdemosrc\mainwindow
ui
彭波39614 小时前
.NET Framework 3.5问题修复教程!可以离线修复
windows·安全·电脑·.net·开源软件
武藤一雄21 小时前
WPF处理耗时操作的7种方法
microsoft·c#·.net·wpf