You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI中DevExpress DataGridView如何按条件整行着色?

实现.NET MAUI DevExpress DataGrid整行按条件着色

有两种实用方法可以实现你的需求,分别适配不同开发场景:

方法1:修改CustomCellAppearance事件实现整行着色

你之前的代码仅针对PickedQuantity列处理,只需调整逻辑,对每行的所有单元格统一判断并设置背景色即可:

ItemsToPickingDataGrid.CustomCellAppearance += ItemsToPickingDataGrid_CustomCellAppearance;

// ...

private void ItemsToPickingDataGrid_CustomCellAppearance(object sender, DevExpress.Maui.DataGrid.CustomCellAppearanceEventArgs e)
{
    // 获取当前行的PickedQuantity值并做判断
    if (ItemsToPickingDataGrid.GetCellValue(e.RowHandle, "PickedQuantity") is decimal pickedQuantity && pickedQuantity == 0)
    {
        e.BackgroundColor = Colors.Red;
    }
    else
    {
        // 还原默认背景,避免样式残留到其他行
        e.BackgroundColor = DevExpress.Maui.DataGrid.DataGrid.DefaultBackgroundColor;
    }
}

原理:遍历每个单元格时,都检查当前行的PickedQuantity值,满足条件就给该单元格设置红色背景,所有单元格同步设置后就会呈现整行红色的效果。

方法2:XAML中使用RowStyle结合DataTrigger(推荐)

如果你的项目遵循MVVM模式,更推荐用声明式的XAML配置,无需编写后台事件:

<dxg:DataGrid x:Name="ItemsToPickingDataGrid">
    <!-- 配置整行样式触发器 -->
    <dxg:DataGrid.RowStyle>
        <Style TargetType="dxg:DataGridRow">
            <Style.Triggers>
                <!-- 当PickedQuantity等于0时,设置整行背景色 -->
                <DataTrigger Binding="{Binding PickedQuantity}" Value="0">
                    <Setter Property="BackgroundColor" Value="Red" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </dxg:DataGrid.RowStyle>

    <!-- 你的列定义 -->
    <dxg:DataGrid.Columns>
        <dxg:TextColumn FieldName="PickedQuantity" />
        <!-- 其他列... -->
    </dxg:DataGrid.Columns>
</dxg:DataGrid>

这种方式直接通过数据绑定触发样式,代码更简洁,也更易维护。

内容的提问来源于stack exchange,提问作者Furkan Koc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 01:36:05