gpt4 book ai didi

c# - 多个 ItemsSource 集合绑定(bind)

转载 作者:可可西里 更新时间:2023-11-01 08:21:34 25 4
gpt4 key购买 nike

如何将多个不同类型的集合绑定(bind)到 ItemsControl 的 ItemsSource?

使用单一绑定(bind)效果很好:

<ItemsControl ItemsSource="{Binding Foo}" />

但是当我尝试 CompositeCollection 时,Foo 中的项目没有显示:

    <ItemsControl>
<ItemsControl.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Foo}" />
</CompositeCollection>
</ItemsControl.ItemsSource>
</ItemsControl>

最佳答案

我建议将 ListBox 绑定(bind)到您在代码中构建的 CompositeCollection。在此示例中,我使用的是 ViewModel,但您也可以在代码隐藏中执行相同的操作。您可以通过谷歌找到许多关于如何为 ViewModel 实现 ViewModelBase 和 DelegateCommand 的示例。

下面是这个例子的分解:

  • 此示例将 Customer 和 Person 对象加载到两个 ObservableCollection 容器中以支持修改集合。
  • ListBox 将其 ItemsSource 绑定(bind)到包含两个 ObservableCollection 的 CompositeCollection (ObjectCollection)。
  • ListBox 还将其 SelectedItem 绑定(bind)到一个对象 (SelectedObject) 以支持两种基本类型。
  • Button 添加了一个新的 Person 以表明您可以修改 CompositeCollection。
  • 为了完整性,我在最后添加了 Customer 和 Person 定义。

这是 View :

<Window x:Class="StackOverflow.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Main Window" Height="400" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ListBox Grid.Row="0"
SelectedItem="{Binding Path=SelectedObject}"
ItemsSource="{Binding Path=ObjectCollection}">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding FirstName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Grid.Row="1" Content="Add Person" Command="{Binding Path=AddPerson}"/>
</Grid>
</Window>

这是 View 模型:

using System.Collections.Generic;
using System.Windows.Data;
using System.Windows.Input;
using ContextMenuNotFiring.Commands;
using ContextMenuNotFiring.Models;

namespace StackOverflow.ViewModels
{
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
AddPerson = new DelegateCommand<object>(OnAddPerson, CanAddPerson);

CollectionContainer customers = new CollectionContainer();
customers.Collection = Customer.GetSampleCustomerList();

CollectionContainer persons = new CollectionContainer();
persons.Collection = Person.GetSamplePersonList();

_oc.Add(customers);
_oc.Add(persons);
}

private CompositeCollection _oc = new CompositeCollection();
public CompositeCollection ObjectCollection
{
get { return _oc; }
}

private object _so = null;
public object SelectedObject
{
get { return _so; }
set
{
_so = value;
}
}

public ICommand AddPerson { get; set; }
private void OnAddPerson(object obj)
{
CollectionContainer ccItems = _oc[1] as CollectionContainer;
if ( ccItems != null )
{
ObservableCollection<Person> items = ccItems.Collection as ObservableCollection<Person>;
if (items != null)
{
Person p = new Person("AAAA", "BBBB");
items.Add(p);
}
}
}

private bool CanAddPerson(object obj)
{
return true;
}
}
}

模型如下:

public class Customer
{
public String FirstName { get; set; }
public String LastName { get; set; }

public Customer(String firstName, String lastName)
{
this.FirstName = firstName;
this.LastName = lastName;
}

public static ObservableCollection<Customer> GetSampleCustomerList()
{
return new ObservableCollection<Customer>(new Customer[4] {
new Customer("Charlie", "Zero"),
new Customer("Cathrine", "One"),
new Customer("Candy", "Two"),
new Customer("Cammy", "Three")
});
}
}


public class Person
{
public String FirstName { get; set; }
public String LastName { get; set; }

public Person(String firstName, String lastName)
{
this.FirstName = firstName;
this.LastName = lastName;
}

public static ObservableCollection<Person> GetSamplePersonList()
{
return new ObservableCollection<Person>(new Person[4] {
new Person("Bob", "Smith"),
new Person("Barry", "Jones"),
new Person("Belinda", "Red"),
new Person("Benny", "Hope")
});
}
}

关于c# - 多个 ItemsSource 集合绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5445790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com