我刚刚开始使用WPF和MVVM框架。我有一个带有两个DataGrids的Window,我想根据另一个的行选择将数据加载到一个。 有没有人得到任何建议或例子,我尝试了很多方法,但似乎没有任何方法。
谢谢
答案 0 :(得分:3)
看,我可以帮助你一点,你可能需要监控所选项目(通过绑定或事件触发器)。更改时,使用新项目从数据中获取所需信息,然后重新填充第二个数据网格的源集合。
以下是一个可以帮助您的示例代码:
<DataGrid>
SelectedValue="{Binding Path=SelectedValue}"
ItemSource="{Binding Path=Source1}"
/>
<DataGrid>
ItemSource="{Binding Path=Source2}"
/>
</DataGrid>
</DataGrid>
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
}
答案 1 :(得分:1)
这是一个原始但有效的例子,我决定在战地巡回赛之间打字......
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;}
}
}
希望这有帮助。
答案 2 :(得分:1)
我发布了一个简单的代码。您可以根据需要进行更改
查看
<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中的字符串。
我希望解决方案有所帮助。