How to align items in Non Uniform Layout in WinUI3

Sreekanth N Kartha 60 Reputation points
2025-05-18T07:12:06.18+00:00

How to align items using ItemRepeater in a non uniform layout in Winui3.
{9BC39A39-5490-4408-A5C2-42A79A4E9C17}

Here i need some of my tile to take 2 times the width

Developer technologies XAML
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,681 Reputation points
    2025-05-22T17:59:16.2533333+00:00

    Not sure if it is what you want to get, but you can change width in ElementPrepared

    A test by changing width from 100 to 200 for items 2 and 6 in 10 items :

    WinUI3_ItemsRepeater

        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <Grid.RowDefinitions>
                <RowDefinition Height="50"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
            </Grid.RowDefinitions>
    
            <Button x:Name="myButton" Grid.Row="0" Click="MyButton_Click">Click Me</Button>
    
            <ScrollViewer Grid.Row="1" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
                <ItemsRepeater x:Name="ir1" ItemsSource="{x:Bind MyItems}">
                    <ItemsRepeater.Layout>
                        <LinedFlowLayout/>
                    </ItemsRepeater.Layout>
    
                    <ItemsRepeater.ItemTemplate>
                        <DataTemplate x:DataType="x:String">
                            <Border x:Name="ItemBorder"
                                Width="100"
                                Height="100"
                                Margin="5"
                                Background="Blue"/>
                        </DataTemplate>
                    </ItemsRepeater.ItemTemplate>
                </ItemsRepeater>
            </ScrollViewer>
        </Grid>
    
    
       public ObservableCollection<string> MyItems { get; } = new();
    
    
      private async void MyButton_Click(object sender, RoutedEventArgs e)
      {      
          {
              for (int i = 0; i < 10; i++)
              {
                  MyItems.Add($"Item {i}");
              }
    
              ir1.ElementPrepared += ir1_ElementPrepared;
              return;
          }
      }
      
      private void ir1_ElementPrepared(ItemsRepeater sender, ItemsRepeaterElementPreparedEventArgs args)
      {
          int nIndex = sender.GetElementIndex(args.Element);
          if (args.Element is FrameworkElement element)
          {
              if (element.FindName("ItemBorder") is Border border)
              {
                  Test items 2 and 6 with Width = 200 and in Red
                  if (nIndex == 2 || nIndex == 6)
                  {
                      border.Width = 200;
                      border.Background = new SolidColorBrush(Microsoft.UI.Colors.Red);
                  }
                  else
                  {
                      border.Width = 100;
                      border.Background = new SolidColorBrush(Microsoft.UI.Colors.Blue);
                   }
              }
          }
      }
    
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.