如何创建可重用的WPF网格布局

zen*_*dar 21 wpf grid layout

我有一个带有标签控件和页数的窗口 - 标签项.每个选项卡项具有相同的网格布局 - 6行和4列.现在,每个选项卡项都包含具有行和列定义的网格,因此几乎一半的XAML是网格的定义.

如何在一个位置定义此网格并在我的应用程序中重用该定义?模板?用户控制?

除了6x4,我还有两个重复的网格尺寸:8x4和6x6.

编辑:
忘记提及:每个选项卡的网格控件都不同.我只想在某些资源中定义一次网格,以便我可以在不同的标签页上重复使用它们.现在XAML看起来像这样:

    <TabControl>
        <TabItem Header="Property">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition /> 
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <!-- some controls here -->
            </Grid>
        </TabItem>
        <TabItem Header="Style">
            <Grid >
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />                        
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />                        
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <!-- some controls here -->
            </Grid>
        </TabItem>

       ... and this repeats for several more tab items

    </TabControl>
Run Code Online (Sandbox Code Playgroud)

对于表单上的每个选项卡项,将重复此网格定义.令我很恼火的是,XAML的一半是网格定义.

有没有办法在一个地方定义这个网格,然后重用该定义?

Eli*_*bel 33

在我看来,最好的办法是使用ItemsControlItemsPanelTemplate,因为你需要为多个项目的容器:

<FrameworkElement.Resources>
    <Style x:Key="GridItemsStyle"
           TargetType="ItemsControl">
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition />
                            <ColumnDefinition />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                    </Grid>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</FrameworkElement.Resources>
<TabControl>
    <TabItem>
        <ItemsControl Style="{StaticResource GridItemsStyle}">
            <TextBlock Grid.Row="1" Text="R1" />
            <TextBlock Grid.Column="1"
                       Text="C1" />
        </ItemsControl>
    </TabItem>
    <TabItem>
        <ItemsControl Style="{StaticResource GridItemsStyle}">
            <TextBlock Grid.Row="2"
                       Text="R2" />
            <TextBlock Grid.Column="2"
                       Text="C2" />
        </ItemsControl>
    </TabItem>
</TabControl>
Run Code Online (Sandbox Code Playgroud)


bit*_*onk 0

通常,人们会为进入选项卡的数据编写一个数据模板。该数据模板将包含网格。