- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
如何将多个不同类型的集合绑定(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 的示例。
下面是这个例子的分解:
这是 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/
我在 ListView 上使用 ItemsSource。我现在想使用 SQL 查询执行搜索和过滤项目,因此我重新分配了 ItemsSource;但随后抛出异常: 在使用 ItemsSource 之前,
我有一段代码不能正常工作。如果我执行一次 btnNew 就没有问题。如果我执行两次,我会得到一个错误... Operation is not valid while ItemsSource is in
我是 Binding 和 WPF 的新手,最近我学会了如何使用 Binding 技术创建一个包含多个列的 listBox
我的模型中有以下内容: public class Equipment { public enum Type { Detector, Vegetation
我已经创建了自己的集合并实现了 INotifyCollectionChanged。 public class ObservableSortedSet : SortedSet, INotifyColle
我有一个包含项目数组的 View 模型: public class FooViewModel { public FooListItem[] ListItems { get; set; }
如果 DataContext 更改 TabControl 没有反应 现在我必须在这里做每一个变化 tabControlRoom.ItemsSource = (IEnumerable)new Res
如何将枚举设置为 xaml 中的列表框。但是在列表框中,我需要显示描述而不是枚举的名称/值。然后,当我单击一个按钮时,我需要将选定的枚举通过 icommand 作为枚举而不是字符串传递给方法。 例如:
将组合框的 ItemsSource 设置为整数数组? 最佳答案 0 1 2
我的 TreeView 绑定(bind)到 ObservableCollection 并使用 HierarchicalDataTemplates。 我从网络服务中获取模型。 只有当用户单击树中的节点时
我的 WPF 应用程序中有一个 Datagrid 控件,我正在尝试将该控件绑定(bind)到我的主窗口类中的 ObservableCollection 属性。我试图绑定(bind)的属性定义为: pr
我正在写一个自定义 ItemsControl (一个选项卡式文档容器),其中每个项目(选项卡)都可以在用户关闭它时从 UI 中移除。但是,我不能直接从 ItemsControl.Items 中删除它。
这是一些 XAML data:FolderEntity 是一个 LINQ to SQL 数据类,它实现了 INotifyPropertyChanging 和 INotifyProperty
我有一个 Xamarin.Forms(3.2,最新版本)Picker实现为 BindingContext 是这个 ViewModel: public class ClassroomsViewMod
好吧,我正在设计一个自定义 WPF 控件——为了学习——它以类似于 Visual Studio 的方式显示日志消息。我想允许用户通过将消息实例添加到 Items 来添加消息集合,或通过绑定(bind)
我已经阅读了一些帖子,但没有人帮助我解决我的问题。 所以,我有一个带有 Viewmodel 的 View ,并且在 View 内部有一个 DataGrid 绑定(bind)到 viewmodel 内的
我有一个带有 ItemsSource 属性的 UserControl。由于基本的 UserControl 类没有实现 ItemsSource,我必须像这样创建自己的依赖属性: #region Item
在 WPF 中,您可以使用 IValueConverter 或 IMultiValueConverter 将数据绑定(bind)值从 int到 Color. 我有一个模型对象的集合,我想将它们转换为它
我有按钮“添加”和“删除”,但“删除”不起作用。怎么了? 算上我的ObservableCollection已更改,但 ListBox 未更改 示例项目:https://github.com/Vesel
我有一个 UniformGrid .在里面,我想放一个Grid其中包含 2 个 child - 和 Image和 Canvas .我已经有一个 List包含 Grid 的成员有了这个定义。 我正在更新
我是一名优秀的程序员,十分优秀!