DataGrid SelectionChanged MVVM

本文关键字:MVVM SelectionChanged DataGrid | 更新日期: 2023-09-27 18:07:23

我刚开始使用WPF和MVVM框架。我有两个数据网格的窗口,我想在一个基于另一个的行选择加载数据。有人有什么建议或例子吗?我试过很多方法,但似乎都不奏效。

谢谢

DataGrid SelectionChanged MVVM

看,我可以帮你一点忙,你可能需要监视选定的项目(通过绑定或事件触发器)。当它更改为使用新项从数据中获取所需信息,然后重新填充第二个数据网格的源集合时。

下面是一个示例代码,它可以帮助你:

<标题> Xaml h1> 背后的代码

public ObservableCollection Source1 {get;私人设置;}

public ObservableCollection<data> Source2 { get; private set; }
public Data SelectedValue
{
    get { return _selectedValue; }
    set
    {
        if (_selectedValue == value) return;
        _selectedValue = value;
        PopulateSource2();
    }
}
private void PopulateSource2()
{
    Source2.Clear();
    //Get your other data from DB here
    Source2.Add(SelectedValue);//This is just to show that it works
}

我正在张贴一个简单的代码。你可以根据自己的需要修改

视图

<Window x:Class="MultipleDataGrid.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <DataGrid Grid.Column="0" ItemsSource="{Binding SourceOne}" SelectedItem="{Binding SelectedItem}" />
        <DataGrid Grid.Column="1" ItemsSource="{Binding SourceTwo}" />
    </Grid>
</Window>

查看代码隐藏

using System.Windows;
namespace MultipleDataGrid
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new ViewModel();
        }
    }
}

视图模型
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Data;
namespace MultipleDataGrid
{
    class ViewModel : INotifyPropertyChanged
    {
        private readonly object _lockOne = new object();
        private readonly object _lockTwo = new object();
        private ObservableCollection<StringValue> _sourceOne;
        public ObservableCollection<StringValue> SourceOne
        { get { return _sourceOne; } }
        private Dictionary<string, List<StringValue>> _sourceTwoList;
        private List<StringValue> _sourceTwo;
        public List<StringValue> SourceTwo
        {
            get { return _sourceTwo; }
            set { _sourceTwo = value; RaisePropertyChanged("SourceTwo"); }
        }
        private StringValue _selectedItem;
        public StringValue SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                _selectedItem = value;
                PopulateDataGridTwo(value.Value);
                RaisePropertyChanged("SelectedItem");
            }
        }
        private void PopulateDataGridTwo(string key)
        {
            if (_sourceTwoList.ContainsKey(key))
            {
                SourceTwo = _sourceTwoList[key];
            }
        }

        public ViewModel()
        {
            _sourceOne = new ObservableCollection<StringValue>
                {
                    new StringValue("Key1"),new StringValue("Key2"),new StringValue("Key3")
                };
            _sourceTwoList = new Dictionary<string, List<StringValue>>();
            BindingOperations.EnableCollectionSynchronization(_sourceOne, _lockOne);
            BindingOperations.EnableCollectionSynchronization(_sourceTwoList, _lockTwo);
            _sourceTwoList.Add("Key1", new List<StringValue> { new StringValue("KVOneOne"),new StringValue("KVOneTwo") });
            _sourceTwoList.Add("Key2", new List<StringValue> { new StringValue("KVTwoOne"),new StringValue("KVTwoTwo") });
            _sourceTwoList.Add("Key3", new List<StringValue> { new StringValue("KVThreeOne"),new StringValue("KVThreeTwo") });
            RaisePropertyChanged("SourceOne");
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propName)
        {
            var pc = PropertyChanged;
            if (pc != null)
                pc(this, new PropertyChangedEventArgs(propName));
        }
    }
    public class StringValue
    {
        public StringValue(string s)
        {
            _value = s;
        }
        public string Value { get { return _value; } set { _value = value; } }
        string _value;
    }
}

我已经使用了这里的代码来显示DataGrid中的字符串。

这是我决定在《战地》回合之间输入的一个粗糙但有效的例子…

XAML:

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication3"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:Vm />
    </Window.DataContext>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
        </Grid.ColumnDefinitions>
        <DataGrid x:Name="Selector" ItemsSource="{Binding Source}" />
        <DataGrid Grid.Column="1" ItemsSource="{Binding SelectedItem, ElementName=Selector}" />
    </Grid>
</Window>
代码:

namespace WpfApplication3
{
    public class Vm
    {
        public ObservableCollection<ObserverableGrouping> Source { get; set; }
        public Vm()
        {
            Source = new ObservableCollection<ObserverableGrouping>() { 
                new ObserverableGrouping("Group1"){ new ObjectModel() { Name = "A", Description = "Group1 Object1" }, new ObjectModel() { Name = "B", Description = "Group1 Object2" } },
                new ObserverableGrouping("Group2"){ new ObjectModel() { Name = "C", Description = "Group2 Object1" }, new ObjectModel() { Name = "D", Description = "Group2 Object2" } }
            };
        }
    }
    public class ObserverableGrouping : ObservableCollection<ObjectModel>
    {
        public string GroupDescription { get; set; }
        public ObserverableGrouping(string Name)
        {
            this.GroupDescription = Name;
        }
    }
    public class ObjectModel
    {
        public string Name {get;set;}
        public string Description {get;set;}
    }
}