Setting Grid RowDefinitions / ColumnDefinitions Properties in Style

Suppose you have two or more grids that have the same structure (the number of rows and columns is equal).

Can I create a Style and set RowDefinitions / ColumnDefinitions ?

I have tried this. But I get this exception when the application tries to parse xaml:

Xamarin.Forms.Xaml.XamlParseException: Position 14: 9. The value of the property is null or not IEnumerable

My source code:

<StackLayout>
    <StackLayout.Resources>
        <ResourceDictionary>
            <Style TargetType="Grid">
                <Setter Property="RowDefinitions">
                    <Setter.Value>
                        <RowDefinition />
                        <RowDefinition />
                    </Setter.Value>
                </Setter>
            </Style>
        </ResourceDictionary>
    </StackLayout.Resources>

    <Grid>
        <Label Grid.Row="0" Text="(Grid #1) Row #1" />
        <Label Grid.Row="1" Text="(Grid #1) Row #2" />
    </Grid>

    <Grid>
        <Label Grid.Row="0" Text="(Grid #2) Row #1" />
        <Label Grid.Row="1" Text="(Grid #2) Row #2" />
    </Grid>

    <Grid>
        <Label Grid.Row="0" Text="(Grid #3) Row #1" />
        <Label Grid.Row="1" Text="(Grid #3) Row #2" />
    </Grid>
</StackLayout>

Position 14: 9 in the exception refers to the first tag <RowDefinition />.

+4
source share
5

. RowDefinitionCollection RowDefinition.

:

<StackLayout>
    <StackLayout.Resources>
        <ResourceDictionary>
            <Style TargetType="Grid">
                <Setter Property="RowDefinitions">
                    <Setter.Value>
                        <RowDefinitionCollection>
                            <RowDefinition />
                            <RowDefinition />
                        </RowDefinitionCollection>
                    </Setter.Value>
                </Setter>
            </Style>
        </ResourceDictionary>
    </StackLayout.Resources>

    <Grid>
        <Label Grid.Row="0" Text="(Grid #1) Row #1" />
        <Label Grid.Row="1" Text="(Grid #1) Row #2" />
    </Grid>

    <Grid>
        <Label Grid.Row="0" Text="(Grid #2) Row #1" />
        <Label Grid.Row="1" Text="(Grid #2) Row #2" />
    </Grid>

    <Grid>
        <Label Grid.Row="0" Text="(Grid #3) Row #1" />
        <Label Grid.Row="1" Text="(Grid #3) Row #2" />
    </Grid>
</StackLayout>
+1

RowDefinition ColumnDefinition , . , , . ColumnDefinitionCollection a RowDefinitionCollection, , , .

<ResourceDictionary>
    <ColumnDefinitionCollection
        x:Key="MyColumnDefinitionCollection">
        <ColumnDefinition
            Width="50" />
        <ColumnDefinition
            Width="100" />
        <ColumnDefinition
            Width="150" />
    </ColumnDefinitionCollection>
    <RowDefinitionCollection
        x:Key="MyRowDefinitionCollection">
        <RowDefinition
            Height="50" />
        <RowDefinition
            Height="50" />
        <RowDefinition
            Height="100" />
        <RowDefinition
            Height="100" />
    </RowDefinitionCollection>
</ResourceDictionary>
<Grid 
    ColumnDefinitions="{StaticResource MyColumnDefinitionCollection}"
    RowDefinitions="{StaticResource MyRowDefinitionCollection}">
</Grid>
+4

, Grid

public class MyGrid : Grid

, xamls

<local:MyGrid> 

j.f,

+3

@j.f., . , :

The trick added an ad to a separate resource and referred to it inside the style.

<StackLayout>
    <StackLayout.Resources>
        <ResourceDictionary>
            <RowDefinitionCollection x:Key="MyRowDefinitionCollection">
                <RowDefinition />
                <RowDefinition />
            </RowDefinitionCollection>

            <Style TargetType="Grid">
                <Setter Property="RowDefinitions" Value="{StaticResource MyRowDefinitionCollection}" />
            </Style>
        </ResourceDictionary>
    </StackLayout.Resources>

    <Grid>
        <Label Grid.Row="0" Text="(Grid #1) Row #1" />
        <Label Grid.Row="1" Text="(Grid #1) Row #2" />
    </Grid>

    <Grid>
        <Label Grid.Row="0" Text="(Grid #2) Row #1" />
        <Label Grid.Row="1" Text="(Grid #2) Row #2" />
    </Grid>

    <Grid>
        <Label Grid.Row="0" Text="(Grid #3) Row #1" />
        <Label Grid.Row="1" Text="(Grid #3) Row #2" />
    </Grid>
</StackLayout>
+3
source

No, you cannot and, in my opinion, should not.

A grid definition is not a style, but a layout.

From version Xamarin.Forms version 3, more CSS style styling features will be introduced. Perhaps then you can style the grid even more, as in HTML / CSS. But little is known about what will be implemented and how.

0
source

Source: https://habr.com/ru/post/1683706/


All Articles