我刚刚从WPF和MVVM框架开始。我有一个带有两个DataGrids的窗口,我希望根据另一个窗口的行选择将数据加载到一个窗口中。有没有人有任何建议或例子,我已经尝试了许多方法,但似乎没有任何解决办法。
谢谢
发布于 2014-06-06 22:37:03
听着,我可以帮你一点,你可能需要监视所选的项目(不管是绑定还是事件触发器)。当它更改为使用新项从数据中获取所需信息时,然后重新填充第二个数据网格的源集合。
下面是一个可以帮助您的示例代码:
Xaml
<DataGrid SelectedValue="{Binding Path=SelectedValue}"
ItemSource="{Binding Path=Source1}"/>
<DataGrid ItemSource="{Binding Path=Source2}"/>代码背后
公共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
}发布于 2014-06-07 04:25:50
我正在发布一个简单的代码。你可以根据你的需要改变它。
视图
<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中显示字符串。
我希望这个解决方案能有所帮助。
发布于 2014-06-07 02:29:40
这是一个粗糙但有效的例子,我决定在战场之间输入这个例子.
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;}
}
}希望这能有所帮助。
https://stackoverflow.com/questions/24091474
复制相似问题