How to create grid lists with C# in WPF?

Asked

Viewed 161 times

0

I’m new in C#. I wonder how to create a grid list that contains images, something like what android has:

1]

Thanks in advance.

1 answer

0

the content must be placed within a ItemsControl thus:

<ItemsControl 
            ItemsSource="{Binding Tasks, UpdateSourceTrigger=PropertyChanged}"  
            ScrollViewer.CanContentScroll="True"
            ScrollViewer.VerticalScrollBarVisibility="Auto"
            ScrollViewer.HorizontalScrollBarVisibility="Auto"
            >
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Orientation="Horizontal"  
                    AllowDrop="True" 
                    Width="{Binding ActualWidth, ElementName=itmWrapPanel}" 
                    Height="{Binding ActualHeight, ElementName=itmWrapPanel}"  />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate  >
            <local:ucTask  />  <!-- esse ucTask é um usercontrol com o que será exibido -->
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

ucTask

<UserControl x:Class="blablabla.ucTask"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
>
    <Grid Focusable="False" >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0" Margin="0,5">
            <Image  Source="{Binding Imagem}"  Focusable="False"/>
        </Grid>
        <Grid Grid.Row="1" HorizontalAlignment="Center">
            <Label Content="{Binding Texto}" FontSize="10"/>
        </Grid>
    </Grid>
</UserControl>

P.S.: Positioning and/or alignment customizations are not in this example, and are on their own

P.S.2: The WrapPanel is who will distribute the items in your ItemsControl alignment and positioning should be checking on it tb

Browser other questions tagged

You are not signed in. Login or sign up in order to post.