我有一些收藏。例如,List 1
和List 2
。两者都是List<Object>
。
我需要做什么:
1)将它们插入Datagrid
:
2) 为 . 添加新项目Lists
。例如,表单上有一些按钮。我单击它,新项目正在添加到第一个列表中。Datagrid
现在看起来像这样:
3)以某种方式。当我想将内容传递Datagrid
给我的类对象时,程序必须知道List 1
现在包含 2 个项目,但是List 2
- 1 个项目。
我怎样才能以最好的方式执行这些功能?
我有一些收藏。例如,List 1
和List 2
。两者都是List<Object>
。
我需要做什么:
1)将它们插入Datagrid
:
2) 为 . 添加新项目Lists
。例如,表单上有一些按钮。我单击它,新项目正在添加到第一个列表中。Datagrid
现在看起来像这样:
3)以某种方式。当我想将内容传递Datagrid
给我的类对象时,程序必须知道List 1
现在包含 2 个项目,但是List 2
- 1 个项目。
我怎样才能以最好的方式执行这些功能?
这是一个样本...
public class ViewModel : INotifyPropertyChanged
{
public ObservableCollection<SomeItem> VmList { get; set; }
List<SomeItem> List1 = new List<SomeItem>();
List<SomeItem> List2 = new List<SomeItem>();
public ViewModel()
{
// VmList is the item source for the grid
VmList = new ObservableCollection<SomeItem>();
// create two lists
for (int i = 0; i < 10; i++)
{
List1.Add(new SomeItem{ID = "1", Name = "Name " + i});
}
for (int i = 0; i < 10; i++)
{
List1.Add(new SomeItem { ID = "2", Name = "Name (2) " + i });
}
// merge the two separate lists
VmList.AddRange(List1);
VmList.AddRange(List2);
// get the view
var lcv = CollectionViewSource.GetDefaultView(VmList);
// apply a filter
lcv.Filter = o =>
{
var someItem = o as SomeItem;
if (someItem != null)
{
return someItem.ID == "2";
}
return false;
};
}
#region INotifyPropertyChanged Implementation
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string name)
{
var handler = System.Threading.Interlocked.CompareExchange(ref PropertyChanged, null, null);
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
#endregion
}
public class SomeItem:INotifyPropertyChanged
{
private string _id;
public string ID
{
[DebuggerStepThrough]
get { return _id; }
[DebuggerStepThrough]
set
{
if (value != _id)
{
_id = value;
OnPropertyChanged("ID");
}
}
}
private string _name;
public string Name
{
[DebuggerStepThrough]
get { return _name; }
[DebuggerStepThrough]
set
{
if (value != _name)
{
_name = value;
OnPropertyChanged("Name");
}
}
}
#region INotifyPropertyChanged Implementation
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string name)
{
var handler = System.Threading.Interlocked.CompareExchange(ref PropertyChanged, null, null);
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
#endregion
}
public static class Extensions
{
public static void AddRange<T>(this ObservableCollection<T> collection, IEnumerable<T> list)
{
foreach (T t in list)
{
collection.Add(t);
}
}
}
在这个例子中(这是人为的),View Model 构造函数创建了两个列表并将它们添加到绑定到数据网格的 observable 集合中。
然后检索底层集合视图源并将过滤器附加到它。
在您的应用程序中,过滤器将应用于按钮事件处理程序而不是 Vm 构造函数。这只是解释其工作原理的示例。在我最初的评论中,我注意到您也可以使用 LINQ zip 运算符,但是我包含了一个扩展方法,这可能在此刻更有价值。它被称为“AddRange”。
这种方法将允许您将两个列表呈现为一个集合,同时在幕后保持它们各自的身份。它还展示了如何使用过滤器。
集合视图源的文档在这里http://msdn.microsoft.com/en-us/library/System.Windows.Data.CollectionViewSource.aspx
您可以使用CompositeCollection ,它可以轻松绑定到多个集合。如果您想对其中一个集合进行数据绑定,则
CompositeCollection
没有,您必须使用所需的 DataContext 引用 a。例如,您可以创建 CollectionViewSource并绑定是这样的源:DataContext
FrameworkElement
<DataGrid.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Source={StaticResource Collection1}}" />
<CollectionContainer Collection="{Binding Source={StaticResource Collection2}}"/>
</CompositeCollection>
</DataGrid.ItemsSource>