WPF - DataTemplate without a container, elements as rows in a grid?

Is there an equivalent mechanism for ItemsControl.ItemTemplatethat that works with Grid? I have a set of elements, and I would like to present them as strings in Grid, so that I can assign to Grid.Columnindividual elements inside the template (as opposed to strings in a list control). Is this possible in WPF using standard controls?

+3
source share
2 answers

Ok, use ItemControl with the attached property Grid.IsSharedSizeScope="true". Further, for your ItemTemplate, you use the <Grid>same as usual, but now, when you add ColumnDefinition, you set an attribute SharedSizeGroupfor a name unique to each column. For example:

<ItemsControl Grid.IsSharedSizeScope="true">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition SharedSizeGroup="MyFirstColumn" />
                    <ColumnDefinition SharedSizeGroup="MySecondColumn" />
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{Binding MyFirstProperty}"/ >
                <TextBlock Grid.Column="1" Text="{Binding MySecondProperty}"/ >
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

For more information about IsSharedSizeScope and SharedSizeGroup, see this section of the SDK . It should be noted that RowDefinitions also has a SharedSizeGroup so you can execute horizontal layouts.

+12
source

I may have misunderstood your problem, but is it not something like a GridView ?

+1
source

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


All Articles