2013-04-02 49 views
1

我有這樣一類的事:WPF綁定列表框中包含子可枚舉

public class UIThing { 
    public string Name{get;set} 
    public IEnumerable<Thing> LotsOfThings; 
} 

我有一些這樣的ListList<UIThings>),我想將它們裝訂成ListBox使LotsOfThings成員擴展爲ListBox中的項目。就像我想的列表清單一樣。但我無法繞過需要的DataTemplate

任何想法?

+1

考慮使用GridView和列中的一個組合框。 – Paparazzi

+0

聽起來像一個'TreeView'將是適當的。 –

回答

0

這可能給你的想法去做:

我建議你使用ItemsControl

public class UIThing 
{ 
    public string Name { get; set; } 
    public List<string> LotsOfThings { get; set; } 
} 

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    UIThing item1 = new UIThing() { Name = "A", LotsOfThings = new List<string>() { "1A", "2A", "3A", "4A" } }; 
    UIThing item2 = new UIThing() { Name = "B", LotsOfThings = new List<string>() { "1B", "2B", "3B", "4B" } }; 
    UIThing item3 = new UIThing() { Name = "C", LotsOfThings = new List<string>() { "1C", "2C", "3C", "4C" } }; 
    UIThing item4 = new UIThing() { Name = "D", LotsOfThings = new List<string>() { "1D", "2D", "3D", "4D" } }; 

    var list = new List<UIThing>() { item1, item2, item3, item4 }; 
    itemsControl.ItemsSource = list; 
} 

這是XAML:

<ItemsControl Name="itemsControl" HorizontalAlignment="Left" Width="100"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Expander Header="{Binding Name}" Margin="0,5" Width="auto"> 
       <ListBox Width="auto" ItemsSource="{Binding LotsOfThings}" Margin="20,0,0,0" Background="AliceBlue"></ListBox> 
      </Expander> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

結果:

enter image description here

編輯:這裏是ListBox的版本

<ListBox Name="itemsControl" Margin="0,0,197,0"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <Label Content="{Binding Name}" Margin="0,5"></Label> 
       <Border Margin="20,0,0,0" Background="AliceBlue" CornerRadius="10"> 
        <ListBox Width="auto" ItemsSource="{Binding LotsOfThings}" ></ListBox> 
       </Border> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
+0

謝謝,我真正想要的,但沒有說清楚,是我想要在一個列表框中的所有項目。不過,我現在將結果集展平成一個staright列表並顯示它。 –

0

可以使用GridView可以組合框

<GridViewColumn Width="100"> 
    <GridViewColumnHeader Content="Main"/> 
    <GridViewColumn.CellTemplate> 
     <DataTemplate> 
      <ComboBox ItemsSource="{Binding Path=GroupsMain, Mode=OneWay}" DisplayMemberPath="Name" IsEditable="False" SelectedIndex="0" /> 
     </DataTemplate> 
    </GridViewColumn.CellTemplate> 
</GridViewColumn> 

如果你需要在一列,然後一個TreeView一切的建議BYT馬特

0

你需要一個DataTemplate爲您UIThing。在該模板中,您將有另一個綁定到您的LotsOfThings列表的ListView。然後你也可以爲你的Thing類添加一個DataTemplate。