Avalonia中可通过RowStyle绑定DataGridRow的Background属性实现条件行背景色,支持ViewModel属性绑定、IValueConverter转换、LoadingRow事件动态设置及隔行变色;需确保引入DataGrid主题且绑定值为IBrush类型。

Avalonia的DataGrid没有直接的RowStyle属性,但可通过DataGrid.RowStyle绑定一个Style实现逐行样式控制。关键在于目标类型设为DataGridRow,并在其中用Binding关联数据模型中的属性(如IsWarning、Status等)。
RowBackground),或用IValueConverter将业务状态转为BrushTargetType="DataGridRow",再用Setter绑定Background到该属性:<setter property="Background" value="{Binding RowBackground}"></setter>
Trigger:当IsSelected为True时覆盖背景色,避免与数据色冲突对于无法预设属性的场景(比如运行时计算、外部状态联动),可用LoadingRow事件在行加载时注入样式。
LoadingRow="OnDataGridLoadingRow"
if (e.Row.DataContext is Person p && p.Age > 65) e.Row.Background = Brushes.Orange;
无需每行单独绑定,也能快速实现基础视觉区分。
RowBackground="White" 和 AlternatingRowBackground="#F8F8F8"
RowStyle或LoadingRow中设置的值AlternatingRowBackground设为Transparent或与主背景一致即可部分样式行为容易被忽略,导致预期失效。
<styleinclude source="avares://Avalonia.Controls.DataGrid/Themes/Fluent.xaml"></styleinclude>,否则RowStyle可能不生效Background时,值必须是IBrush类型(如Brushes.Red、new SolidColorBrush(Colors.Blue)),不能直接传Color或字符串AutoGenerateColumns="True",仍可应用RowStyle——它作用于整行容器,与列生成逻辑无关以上就是Avalonia如何自定义DataGrid的行样式 Avalonia DataGrid RowStyle的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号