如何在WPF中制作可重复的模板

时间:2013-06-03 03:34:15

标签: c# wpf wpf-controls

我想像这张照片一样制作WPF应用程序..

enter image description here

我想要像数据库结构一样使用数据输入应用程序(列)

A  | B |  C |  D |  ...  | Z

我想要使可重复部分插入列2和列3是否可选。所以它就像我的数据库中的水平行。

如何在WPF中使用数据绑定进行这样的结构布局?我想让我的Textbox A将数据绑定到我的数据库中的A文本框B将与B等绑定..

如何做到这一点的最佳方法......?救救我..

1 个答案:

答案 0 :(得分:0)

看起来你只是想显示一个列表列表。

您可以将数据库表简化为以下内容:

Id | ColumnIndex | RowIndex | Value

您的查询可能如下所示:

GroupedValues = table.GroupBy(x => x.ColumnIndex);

然后你可以使用ItemsControl将ItemsPanel设置为水平方向的StackPanel,将ItemTemplate设置为标准(垂直)ItemsControl,如下所示:

<ItemsControl ItemsSource="{Binding GroupedValues}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Value}" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

此代码未经过测试,但希望它能为您提供这个想法。