我如何绑定元素列表的数组

本文关键字:元素 列表 数组 绑定 何绑定 | 更新日期: 2023-09-27 18:15:25

我有以下代码在ViewModel -

List<Class1> categories = new List<Class1>
Class1 ob1 = new Class1(){
    Heading = "H1",
    Commands = new VMCommand[]
    {
        new VMCommand("Command11", CanExecuteCommand11, ExecuteCommand11),
        new VMCommand("Command12", CanExecuteCommand12, ExecuteCommand12),
        new VMCommand("Command13", CanExecuteCommand13, ExecuteCommand13),
        new VMCommand("Command14", CanExecuteCommand14, ExecuteCommand14)
    }
categories.Add(ob1);
Class1 ob2 = new Class1(){
    Heading = "H2",
    Commands = new VMCommand[]
    {
        new VMCommand("Command21", CanExecuteCommand21, ExecuteCommand21),
        new VMCommand("Command22", CanExecuteCommand22, ExecuteCommand22),
        new VMCommand("Command23", CanExecuteCommand23, ExecuteCommand23),
        new VMCommand("Command24", CanExecuteCommand24, ExecuteCommand24)
    }
categories.Add(ob2);

我在Xaml中获得类别。我想从所有类别的所有命令的列表。

我如何绑定元素列表的数组

您可以添加一个提取所有命令的属性或方法,然后绑定到它。我还没有编译这个,但是你应该能够做这样的事情:

public ObservableCollection<VMCommand> AllCommands
{
    return new ObservableCollection(categories.SelectMany(x => x.Commands));    
}