Tabcontrol contentTemplate定位

时间:2012-01-04 13:47:58

标签: wpf vb.net binding tabcontrol itemtemplate

祝大家新年快乐!感谢您阅读本文!

我在为contentTemplate定位动态内容时遇到问题,我有以下内容:

<TabControl ItemsSource="{Binding Cats}">
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}" FontSize="16" FontWeight="Bold"></TextBlock>
                </DataTemplate>                
            </TabControl.ItemTemplate>
            <TabControl.ContentTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <ItemsControl ItemsSource="{Binding Products}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Button Content="{Binding Name}" Height="25" Width="100"></Button>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </WrapPanel>
                </DataTemplate>
            </TabControl.ContentTemplate>
        </TabControl>

itemsSource绑定猫返回类别列表 在每个类别中都有一个返回产品列表的属性

enter image description here

目前正在显示垂直堆叠的所有按钮,但我需要按钮来填充wrapPanel。我正在寻找contentTemplate的例子:

enter image description here

任何想法都会非常感激!

1 个答案:

答案 0 :(得分:2)

您需要使用WrapPanel作为ItemsPanel模板。请参阅this问题以获取参考。基本上,您需要在以下位置添加此XAML:

<ItemsControl ItemsSource="{Binding Products}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Content="{Binding Name}" Height="25" Width="100"></Button>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

注意:您实际上并不需要设置Orientation的{​​{1}}属性,只是出于明确的原因。