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

如何在.NET MAUI桌面应用中访问DataGrid的SelectedRows属性

解决方案

一、为什么静态方法中无法访问GroupVw?

静态方法属于类级别,不依赖类的实例存在;而GroupVw是MainPage实例化后的控件成员,属于实例级别。静态方法没有当前页面的实例上下文,自然无法直接访问实例控件的属性。构造函数能正常访问是因为它是实例方法,运行在页面实例的上下文中。

二、通过MVVM绑定实现选中行的跨场景访问

绑定SelectedItems是符合MVVM规范的最优方案,既能避免直接操作UI控件,又能让选中数据在ViewModel或其他业务逻辑中轻松调用。具体实现步骤如下:

1. 在ViewModel中定义选中项集合

确保ViewModel实现INotifyPropertyChanged接口(用于属性变更通知),添加存储选中分类的集合属性:

public class MainViewModel : INotifyPropertyChanged
{
    // 分类数据源(假设你的分类实体为Category)
    public ObservableCollection<Category> Categories { get; set; }

    // 选中的分类集合
    private ObservableCollection<Category> _selectedCategories;
    public ObservableCollection<Category> SelectedCategories
    {
        get => _selectedCategories;
        set
        {
            _selectedCategories = value;
            OnPropertyChanged();
            // 可选:选中项变化时自动触发匹配逻辑
            LoadMatchingPasswords();
        }
    }

    // 匹配后的密码数据(绑定到第二个DataGrid)
    public ObservableCollection<Password> MatchingPasswords { get; set; }

    public MainViewModel()
    {
        Categories = new ObservableCollection<Category>();
        SelectedCategories = new ObservableCollection<Category>();
        MatchingPasswords = new ObservableCollection<Password>();
    }

    // 实现INotifyPropertyChanged的必要方法
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    // 从另一张表匹配分类ID的逻辑
    public void LoadMatchingPasswords()
    {
        MatchingPasswords.Clear();
        foreach (var category in SelectedCategories)
        {
            // 替换为你的数据库查询逻辑,根据category.Id匹配Password表数据
            var matchedPasswords = YourDataService.GetPasswordsByCategoryId(category.Id);
            foreach (var pwd in matchedPasswords)
            {
                MatchingPasswords.Add(pwd);
            }
        }
    }
}

2. 在XAML中绑定DataGrid的SelectedItems

修改MainPage的XAML,将第一个DataGrid的SelectedItems绑定到ViewModel的SelectedCategories,按需设置多选模式:

<syncfusion:SfDataGrid x:Name="GroupVw"
                       ItemsSource="{Binding Categories}"
                       SelectedItems="{Binding SelectedCategories, Mode=TwoWay}"
                       SelectionMode="Multiple" />

<!-- 第二个DataGrid绑定匹配后的密码数据 -->
<syncfusion:SfDataGrid ItemsSource="{Binding MatchingPasswords}" />

3. 给页面绑定ViewModel

在MainPage构造函数中设置DataContext:

public MainPage()
{
    InitializeComponent();
    var viewModel = new MainViewModel();
    DataContext = viewModel;
    // 初始化分类数据
    viewModel.Categories = YourDataService.GetAllCategories();
}

4. 在其他地方访问选中数据

如果需要在静态方法或其他类中调用选中数据,有两种可行方式:

  • 方式一:通过ViewModel实例访问
    若能获取MainPage的ViewModel实例,直接访问SelectedCategories即可:
    // 假设已拿到viewModel实例
    var selectedCategories = viewModel.SelectedCategories;
    foreach (var category in selectedCategories)
    {
        // 执行业务逻辑
    }
    
  • 方式二:将ViewModel设为全局单例
    如需多场景共享ViewModel,可设计为单例:
    public class MainViewModel : INotifyPropertyChanged
    {
        private static MainViewModel _instance;
        public static MainViewModel Instance => _instance ??= new MainViewModel();
    
        // 其他代码不变...
    }
    
    静态方法中直接调用:
    public static void GetPasswrds()
    {
        var selectedCategories = MainViewModel.Instance.SelectedCategories;
        foreach (var category in selectedCategories)
        {
            var matchedPasswords = YourDataService.GetPasswordsByCategoryId(category.Id);
            // 处理匹配数据
        }
    }
    

三、特殊场景下直接访问控件(不推荐)

若因特殊需求必须直接操作GroupVw,可在MainPage中保存当前实例的静态引用,但这种方式违反MVVM原则,会增加代码耦合度:

public partial class MainPage : ContentPage
{
    private static MainPage _currentInstance;
    public static MainPage CurrentInstance => _currentInstance;

    public MainPage()
    {
        InitializeComponent();
        _currentInstance = this;
    }

    public static void GetPasswrds()
    {
        if (_currentInstance != null)
        {
            var selectedRows = _currentInstance.GroupVw.SelectedRows;
            foreach (var row in selectedRows)
            {
                var category = row.DataContext as Category;
                // 执行匹配逻辑
            }
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 11:55:36