在列表列表中的组合框项目中显示列表

时间:2018-09-18 08:14:46

标签: c# wpf combobox binding

我目前有一个List<List<Path>>,我想在其中显示组合框项目中的内部列表。大致像这样的伪:

<ComboBox>
    <ComboBoxItem Content="3 paths"/>
    <ComboBoxItem Content="3 paths"/>
    <ComboBoxItem Content="3 paths"/>
    <ComboBoxItem Content="3 paths"/>
</Combobox>

编辑:添加了图像

这就是现在的样子

Current combobox

这大概就是我想要的

enter image description here

在图像中,每行是一个列表,其中包含另一个包含三角形的列表。

因此,如果最外面的列表中有4个列表项,则每个列表都有3条路径,我希望它们如上所示。 我现在的设置就是这样

Xaml:

<ComboBox ItemsSource="{Binding Path=AvailableCombinationsOfShape}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Path Data="{Binding Path=Data}" StrokeThickness="1" Stroke="Black"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

后面的代码:

AvailableCombinationsOfShape = new List<List<Path>>();

foreach (var combination in combinations)
{
    var r = CombineShapes(GetImageShapes(), combination);
    AvailableCombinationsOfShape.Add(r);
    i++;
}      
private List<Path> CombineShapes(List<SymbolShapeModel> shapes, int[] numbersNotToFill)
{
    var pathList = new List<Path>();
    foreach (var shape in shapes)
    {
        var p = new Path();
        p.Data = shape.Shape;
        pathList.Add(p);
    }
    return pathList;
}

有了这个,我得到了要显示在组合框中的每个列表中的第一个形状,但是我想显示所有三个形状。

我想要这样的原因是因为我想为每个组合框项目中的某些形状上色。 (想象3个正方形。我希望第一个项目显示正方形1和2的颜色,第二个项目应该显示正方形2和3的颜色,最后一个项目应该显示正方形1和3的颜色。)

希望有人可以帮助我! 谢谢!

1 个答案:

答案 0 :(得分:1)

谢谢@RobinBennett。我只需要一个Itemscontrol

<ComboBox ItemsSource="{Binding Path=AvailableCombinationsOfShape, Mode=OneWay}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Path Data="{Binding Path=Data}" StrokeThickness="1" Stroke="Black"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

该放置位置对于三角形来说有点奇怪,但这是我能够解决的问题。