I
I
Ignatiy22019-12-02 13:35:53
WPF
Ignatiy2, 2019-12-02 13:35:53

How to dynamically create Grid items?

I have a collection of data and a ListBox associated with it.

<Grid Grid.Column="1">
            <ListBox ItemsSource="{Binding Notes}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Background="#FF81E072">
                                <TextBlock Text="{Binding Title}" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
  </Grid>

I want each "line" of lisbox to have its own cell in the grid table.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Foggy Finder, 2019-12-03
@Ignatiy2

Can be set as a panel UniformGrid , then all rows will have the same height.

<ListBox Grid.Column="1" ItemsSource="{Binding Notes}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="1" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Title}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ListBox>

Thanks for the idea Athari

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question