2013-04-02 54 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。