【问题标题】:DataGrid SelectionChanged MVVM [closed]DataGrid SelectionChanged MVVM [关闭]
【发布时间】:2014-07-28 07:02:05
【问题描述】:

我刚开始使用 WPF 和 MVVM 框架。我有一个带有两个 DataGrid 的窗口,我想根据另一个的行选择将数据加载到一个中。 有没有人有任何建议或例子,我尝试了很多方法,但似乎都没有成功。

谢谢

【问题讨论】:

    标签: c# wpf mvvm telerik


    【解决方案1】:

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

    这是一个可以帮助你的示例代码:

    Xaml

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

    代码隐藏

    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
    }
    

    【讨论】:

      【解决方案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;}
          }
      }
      

      希望这会有所帮助。

      【讨论】:

        【解决方案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;
            }
        }
        

        我已使用here 中的代码在 DataGrid 中显示字符串。

        我希望解决方案有所帮助。

        【讨论】:

          猜你喜欢
          • 2017-07-15
          • 1970-01-01
          • 1970-01-01
          • 2012-06-04
          • 2019-12-22
          • 2015-04-17
          • 2018-05-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多