DataGrid选定项更改 MVVM

3
我刚开始学习WPF和MVVM框架。我有一个带有两个DataGrids的窗口,我想基于另一个DataGrid的行选择加载数据到其中一个DataGrid。 是否有人有任何建议或示例?我已经尝试了许多方法,但似乎都没有成功。
谢谢。
3个回答

4

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

这里有一个示例代码可以帮助你:

Xaml

<DataGrid SelectedValue="{Binding Path=SelectedValue}"
          ItemSource="{Binding Path=Source1}"/>
<DataGrid ItemSource="{Binding Path=Source2}"/>

代码后置

public ObservableCollection Source1 { get; private set; }

注:此段代码为C#语言中的属性定义,表示Source1是一个可观测的集合对象,其可读可写方法为私有。
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
}

3
我会尽力做到最好。以下是您需要翻译的内容:

我发布了一个简单的代码,您可以根据需要进行更改。

查看

<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中显示字符串。

希望这个解决方案能够帮到你。


2

我来提供一个简陋但可行的示例,在战地之间我决定打字...

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;}
    }
}

希望这有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接