1

我有这样的课:

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

List我在( )中有其中一些,List<UIThings>我想将它们绑定到 aListBox中,以便将 lotOfThings 成员扩展为ListBox. 就像我猜的列表一样。但我无法理解DataTemplate需要的东西。

有任何想法吗?

4

3 回答 3

0

可以用GridView 可以ComboBox

<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>

如果您需要单列中的所有内容,那么按照 Matt Matt 的建议使用 TreeView

于 2013-04-02T16:51:49.487 回答
0

您需要为您的UIThing. 在该模板中,您将拥有另一个绑定到您的LotsOfThings列表的 ListView。然后,您也可以为您的Thing班级添加一个 DataTemplate。

于 2013-04-02T16:53:00.733 回答
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>

结果:

在此处输入图像描述

编辑:这是列表框版本

<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>
于 2013-04-02T17:35:49.400 回答