Link the RowDefinition number dynamically to the grid in ItemsPanelTemplate

I wrote xaml to display video files in a 3-column grid. I used xaml as shown below:

<ItemsControl Name="icTodoList" Grid.IsSharedSizeScope="True" ItemsSource="{Binding items}" Grid.Row="0" Grid.Column="0" > <ItemsControl.ItemsPanel> <ItemsPanelTemplate > <Grid x:Name="icTooList" Margin="100,0,100,0" Style="{Binding Path=Style}"> <Grid.ColumnDefinitions> <!--Whatever I do I can't get the screen to resize and the cols to have the same width--> <ColumnDefinition Width="Auto" SharedSizeGroup="A" /> <ColumnDefinition Width="Auto" SharedSizeGroup="A" /> <ColumnDefinition Width="Auto" SharedSizeGroup="A" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> </Grid.RowDefinitions> </Grid> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <Grid Margin="40,0,40,30"> <TextBlock HorizontalAlignment="Center" Margin="0,0,0,0"> <Hyperlink TextDecorations="None" NavigateUri="{Binding UriPath}" RequestNavigate="Hyperlink_RequestNavigate" CommandParameter="{Binding ElementName=myImg}"> <Image Width="120" Height="120" x:Name="myImg" Source="{Binding Source}" Margin="5"/> </Hyperlink> </TextBlock> <TextBlock Margin="0,120,0,0" HorizontalAlignment="Center"> <TextBlock FontSize="20px" Text="{Binding Title}" Foreground="white"></TextBlock> </TextBlock> </Grid> </DataTemplate> </ItemsControl.ItemTemplate> <ItemsControl.ItemContainerStyle> <Style> <Style.Setters> <Setter Property="Grid.Row" Value="{Binding GridRow}" /> <Setter Property="Grid.Column" Value="{Binding GridColumn}" /> </Style.Setters> </Style> </ItemsControl.ItemContainerStyle> </ItemsControl> 

Here I used RowDefinition as below

 <Grid.RowDefinitions> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> </Grid.RowDefinitions> 

I already found the number of RowDefinition requests in the backend, as shown below

  void setaligned() { int currentColumn = 0; int currentRow = 0; foreach (BindingFilesContent checkBoxItem in items) { checkBoxItem.GridColumn = currentColumn; checkBoxItem.GridRow = currentRow; if (currentColumn != 2) { currentColumn++; } else { currentRow++; currentColumn = 0; } } } 

But I need to link this RowDefinition dynamically in the Grid. I tried with the following, but did not work for me. No one answered the question below.

How can I dynamically add RowDefinition to the grid in ItemsPanelTemplate?

0
source share
1 answer

I used UniformGrid and anchored rows, found below code for dynamic row binding.

Xaml:

  <UniformGrid Columns="3" Rows="{Binding RowCount}"> </UniformGrid> 

FROM#

 List<PartList> items1 = new List<PartList>(); int currentRow = 10; items1.Add(new PartList() { RowCount = currentRow }); public class PartList { public int RowCount { get; set; } } 
+1
source

All Articles