Avalonia treedatagrid使用杂记
这里只对最近使用到的分层树做一些记录,有复选框示例,支持父级选中状态改变子集同步变化
废话不多说,直接上源码
View布局
<TreeDataGrid
Height="710"
BorderBrush="Gray"
CanUserResizeColumns="False"
FontSize="16"
Source="{Binding TreeDataGridSource}">
<TreeDataGrid.Resources>
<DataTemplate x:Key="CheckBoxCellTemplate" x:DataType="vm:TreeDataModel">
<CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" />
</DataTemplate>
</TreeDataGrid.Resources>
</TreeDataGrid>
model对象
public class TreeDataModel : INotifyPropertyChanged
{
private string _name; public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged(nameof(Name));
}
} private int? _age; public int? Age
{
get { return _age; }
set
{
_age = value;
OnPropertyChanged(nameof(Age));
}
} public bool _isSelected; public bool IsSelected
{
get { return _isSelected; }
set
{
_isSelected = value;
OnPropertyChanged(nameof(IsSelected)); // 如果是分类节点,更新所有子项的状态
if (IsCategory)
{
foreach (var child in Children)
{
child.IsSelected = value;
}
}
}
} private string _department; public string Department
{
get => _department;
set
{
if (_department != value)
{
_department = value;
OnPropertyChanged(nameof(Department));
}
}
} private string _category; public bool IsCategory => !string.IsNullOrEmpty(Category); public bool IsExpanded { get; set; } = true; public string Category
{
get { return _category; }
set
{
_category = value;
OnPropertyChanged(nameof(Category));
}
} private ObservableCollection<TreeDataModel> _children = new ObservableCollection<TreeDataModel>(); public ObservableCollection<TreeDataModel> Children
{
get { return _children; }
set
{
_children = value;
OnPropertyChanged(nameof(Children));
}
} public event PropertyChangedEventHandler? PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
} protected bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}
}
vm 数据刷新逻辑
private HierarchicalTreeDataGridSource<TreeDataModel> _treeDataGridSource =
new HierarchicalTreeDataGridSource<TreeDataModel>(new List<TreeDataModel>()); public HierarchicalTreeDataGridSource<TreeDataModel> TreeDataGridSource
{
get { return _treeDataGridSource; }
set
{
_treeDataGridSource = value;
OnPropertyChanged(nameof(TreeDataGridSource));
}
} private TreeDataModel selectedItem; public TreeDataModel SelectedItem
{
get => selectedItem;
set
{
selectedItem = value;
OnPropertyChanged(nameof(SelectedItem));
}
} public async void InitTreeData()
{
List<TreeDataModel> data = new List<TreeDataModel>();
for (int i = 0; i < 10; i++)
{
int value = 10 + i;
data.Add(new TreeDataModel()
{
Age = value,
Name = $"示例{value}",
Department = "测试部",
});
} for (int i = 0; i < 7; i++)
{
int value = 10 + i;
data.Add(new TreeDataModel()
{
Age = value,
Name = $"张三{value}",
Department = "研发",
});
} for (int i = 23; i < 50; i++)
{
int value = 10 + i;
data.Add(new TreeDataModel()
{
Age = value,
Name = $"酒香也怕巷子深{value}",
Department = "心如止水",
});
} //聚合形成树形数据集
var categoryGroups = data
.GroupBy(i =>
{
// 按照"-"前面的部分分类
var dashIndex = i.Department.IndexOf('-');
return dashIndex > 0 ? i.Department.Substring(0, dashIndex) : i.Department;
});
List<TreeDataModel> listData = new List<TreeDataModel>();
foreach (var group in categoryGroups)
{
var category = new TreeDataModel { Category = group.Key }; foreach (var item in group)
{
category.Children.Add(item);
} listData.Add(category);
} TreeDataGridSource =
new HierarchicalTreeDataGridSource<TreeDataModel>(listData)
{
Columns =
{
new HierarchicalExpanderColumn<TreeDataModel>(
new TextColumn<TreeDataModel, string>(
"人员",
x => x.IsCategory ? $"{x.Category}({x.Children.Count}成员)" : x.Name,
GridLength.Auto)
,
x => x.Children,
isExpandedSelector: x => x.IsExpanded),
new TextColumn<TreeDataModel, int?>(
"年龄",
x => x.Age,
GridLength.Auto),
new TemplateColumn<TreeDataModel>(
"",
"CheckBoxCellTemplate", // 使用自定义模板
width: GridLength.Auto),
},
}; TreeDataGridSource.RowSelection.SelectionChanged += (s, e) =>
{
if (s is TreeDataGridRowSelectionModel<TreeDataModel> selec)
{
if (selec.SelectedItem is TreeDataModel selectModel)
{
SelectedItem = selectModel;
}
}
};
}
关键点,
1.数据格式绑定基本都在vm完成treedatagrid数据源集合需要绑定HierarchicalTreeDataGridSource,而不是传统的ObservableCollection
2.行选中时间SelectionChanged,控件不支持直接使用SelectionChanged,需要vm业务层订阅RowSelection来引出SelectionChanged,数据集每次变化都需要重新订阅
3.复选框:
数据集Columns绑定时其实有CheckBoxColumn列,不过这里演示通过绑定资源键的形式 x:Key="CheckBoxCellTemplate"实现复选框
父级状态改变影响子集,Avalonia原本的TreeDataGrid并没有对这个实现,但是自己可以通过通过对IsSelected属性变化时,验证IsCategory是否是分类节点 从而更改子集选中状态
Avalonia treedatagrid使用杂记的更多相关文章
- [Erlang 0118] Erlang 杂记 V
我在知乎回答问题不多,这个问题: "对你职业生涯帮助最大的习惯是什么?它是如何帮助你的?",我还是主动回答了一下. 做笔记 一开始笔记软件做的不好的时候就发邮件给自己, ...
- Ubuntu杂记——Ubuntu下用虚拟机共享上网
由于最近把自己电脑环境换成了Ubuntu,但学校的网络是电信的闪讯,大学里用过的人都知道这货有多坑,而且没有Linux客户端,上网都是问题,怪不得国内用Linux的人那么少,特别是高校的学生(让我瞎逼 ...
- 一个ubuntu phper的自我修养(杂记)
ubuntu使用杂记 1.flatabulous安装使用. flatabulous是一个ubuntu图标主题. 使用它,必须得安装tweak插件. sudo add-apt-repository pp ...
- 有关Java的日期处理的一些杂记
在企业应用开发中,经常会遇到日期的相关处理,说实话JDK自带的日期方法很难用.就我个人而言我一般都会采用joda-time来替代JDK自身的日期. 这篇文章是杂记,所以写的比较零散,希望大家不要见怪. ...
- 分布式系统之CAP理论杂记[转]
分布式系统之CAP理论杂记 http://www.cnblogs.com/highriver/archive/2011/09/15/2176833.html 分布式系统的CAP理论: 理论首先把分布式 ...
- Redis杂记
参考资料: Redis 教程 | 菜鸟教程 : http://www.runoob.com/redis/redis-tutorial.html Redis快速入门 :http://www.yiibai ...
- MySQL杂记
参考资料: w3school SQL 教程 : http://www.w3school.com.cn/sql/index.asp 21分钟 MySQL 入门教程 : http://www.cnblo ...
- Android之开发杂记(一)
1.cygwin环境变量设置 可在Cygwin.bat 中设置 set NDK_ROOT=P:/android/android-ndk-r8e 或者在home\Administrator\.bash_ ...
- ios程序开发杂记
ios程序开发杂记 一.程序构建 与一般的程序构建无太大区别,都是源文件编译链接这一套,通常是在mac上做交叉编译,也就是利用xcode里带的ios编译工具集去生成arm架构的ios程序(或是x86的 ...
- .NET Core UI框架Avalonia
.NET Core UI框架Avalonia,Avalonia是一个基于WPF XAML的跨平台UI框架,并支持多种操作系统:Windows(.NET Framework,.NET Core),Lin ...
随机推荐
- TensorFlow 基础 (03)
项目再忙碌, 还是要抽出时间来学习的. 最近到整一些数据清洗小工具, 数据导入数据库工具等... 有种感觉是, 之前我做分析师的时候, 啥工具都没有, 全部我自己造, 数据表整理, 业务整理, 建库建 ...
- C#窗体磁吸屏幕的两种实现方案 - 开源研究系列文章
以前在大学的时候模仿Winamp的磁吸效果编写过一个类库,用于在应用中多个窗体的相互磁吸效果.因为此效果应用不多,但是窗体磁吸屏幕边缘的效果倒是比较实用,于是就用C#来实现窗体磁吸屏幕边缘的代码,这里 ...
- Python标准库之Collections---Container datatype
Deques Deques,即 Double-ended-queues,是支持线程安全,内存高效的列表类对象.Deques是可改变的,支持索引等类似于list的操作,然而,我们不能直接对Deques进 ...
- IPO——LeetCode⑫
//原题链接https://leetcode.com/problems/ipo/ 题目描述 Suppose LeetCode will start its IPO soon. In order to ...
- 通过node.js安装yarn
如果你已经装好了node,那么 1.添加repo curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /e ...
- Java 提取url的域名
有时候,我们需要校验URL的域名是否在白名单中,故需要提取其中的域名.可以使用java标准类库java.net.URL进行提取,方法如下: import org.apache.commons.la ...
- 转|如何从 100 亿 URL 中找出相同的 URL
题目描述 给定 a.b 两个文件,各存放 50 亿个 URL,每个 URL 各占 64B,内存限制是 4G.请找出 a.b 两个文件共同的 URL. 解答思路 每个 URL 占 64B,那么 50 亿 ...
- K8S的API Server认证介绍
一.说明 kube-apiserver是k8s最重要的制组件之一,主要提供以下功能: 提供集群管理的REST API 接口, 包括认证授权.数据校验以及集群状态变更等 k8s 中所有模块与 etcd ...
- 大数据开源项目,一站式全自动化全生命周期运维管家ChengYing(承影)走向何方?
原文链接:三分钟走进袋鼠云一站式全自动化全生命周期运维管家ChengYing(承影) 课件获取:关注公众号 ** "数栈研习社",后台私信 "ChengYing" ...
- 使用 certbot 通过 Let's Encrypt 申请免费证书,部署到 nginx 中,开启 https
使用 certbot 可以很方便.快捷的通过 Let's Encrypt 申请免费的证书,并部署到 nginx 中,开启 https 在 Linux 通过命令安装 安装 Nginx sudo apt ...