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

.NET MAUI DevExpress DataGridView复选框点击获取任务ID及更新问题

解决方案

问题的核心是CheckBox的BindingContext为当前行的Melding实体,而命令定义在ViewModel中,直接绑定会因上下文不匹配导致命令无法找到。通过指定命令的绑定来源为页面的ViewModel,并传递当前Melding作为参数,即可解决这个问题。

步骤1:XAML中配置DataGridView与EventToCommandBehavior

使用DevExpress的TemplateColumn自定义复选框列,通过RelativeSource指定命令的绑定来源为页面的BindingContext(即ViewModel),同时将当前行的Melding实体作为命令参数传递:

<dxg:DataGridView ItemsSource="{Binding MeldingenList}">
    <!-- 其他列的定义 -->
    <dxg:TemplateColumn Caption="Afgewerkt">
        <dxg:TemplateColumn.CellTemplate>
            <DataTemplate>
                <CheckBox IsChecked="{Binding Afgewerkt, Mode=TwoWay}">
                    <CheckBox.Behaviors>
                        <dxmvvm:EventToCommandBehavior 
                            EventName="CheckedChanged"
                            Command="{Binding Source={RelativeSource AncestorType={x:Type ContentPage}}, Path=BindingContext.ChangeAfgewerktCommand}"
                            CommandParameter="{Binding .}" />
                    </CheckBox.Behaviors>
                </CheckBox>
            </DataTemplate>
        </dxg:TemplateColumn.CellTemplate>
    </dxg:TemplateColumn>
</dxg:DataGridView>

关键说明:

  • RelativeSource AncestorType={x:Type ContentPage}:找到页面级别的BindingContext(即你的ViewModel),确保命令能正确绑定到ViewModel中的ChangeAfgewerktCommand。
  • CommandParameter="{Binding .}":将当前行的Melding实体作为参数传递给命令,这样就能直接获取到实体的ID。

步骤2:ViewModel中实现命令

可以选择DevExpress内置的RelayCommand或微软官方的CommunityToolkit.Mvvm工具包实现命令,接收Melding类型的参数完成数据库更新:

用DevExpress Mvvm实现:

using DevExpress.Mvvm;
using System.Collections.ObjectModel;

public class YourViewModel : ViewModelBase
{
    private readonly IMeldingService _meldingService;

    public ICommand ChangeAfgewerktCommand { get; }
    public ObservableCollection<Melding> MeldingenList { get; set; } = new();

    public YourViewModel(IMeldingService meldingService)
    {
        _meldingService = meldingService;
        ChangeAfgewerktCommand = new RelayCommand<Melding>(ExecuteChangeAfgewerkt);
    }

    private async void ExecuteChangeAfgewerkt(Melding melding)
    {
        if (melding == null) return;
        
        // 更新数据库中对应任务的完成状态
        await _meldingService.UpdateCompletionStatus(melding.Id, melding.Afgewerkt);
    }
}

用CommunityToolkit.Mvvm实现:

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Collections.ObjectModel;

public partial class YourViewModel : ObservableObject
{
    private readonly IMeldingService _meldingService;

    [ObservableProperty]
    private ObservableCollection<Melding> _meldingenList = new();

    public YourViewModel(IMeldingService meldingService)
    {
        _meldingService = meldingService;
    }

    [RelayCommand]
    private async Task ChangeAfgewerktAsync(Melding melding)
    {
        if (melding == null) return;
        
        await _meldingService.UpdateCompletionStatus(melding.Id, melding.Afgewerkt);
    }
}

额外注意事项

  1. 确保Melding实体实现了INotifyPropertyChanged接口,这样IsChecked的双向绑定才能实时更新实体的Afgewerkt属性。
  2. 页面的BindingContext必须正确设置为你的ViewModel(可在XAML或代码后台配置)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 15:35:06