2013-05-27 47 views
0

的元素列表我在視圖模型下面的代碼 -我如何可以綁定陣列

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越來越類別。我想用所有類別的所有命令來使用List。

回答

0

您可以添加一個屬性或方法來提取所有命令,然後綁定到該命令。我沒有編譯這個,但你應該可以這樣做:

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

我不想改變ViwModel。我得到了一個提示 - 我試過類別/命令。它給出了第一組的所有命令。你可以探索它嗎? – user2039445