Juan Pablo Gomez
Juan Pablo Gomez

Reputation: 5534

Using GridView with variable width elements

I have a GridView and need to set width elements to Stretch as it's content, for now my Gridview looks like this image

enter image description here

I want something like this:

enter image description here

I think something like change ItemsTemplate to an StackPanel, but some samples found on the web are not enough clear for me.

How can I do to change The ItemsTemplate to get the behavior I want?

or

There is another XAML control that allow me to get this behavior?

Note: I'm using VS 2015, performing a UWP app.

Upvotes: 0

Views: 129

Answers (1)

sexta13
sexta13

Reputation: 1578

check out this:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Window.Resources>

        <DataTemplate x:Key="myTemplate"> 
            <StackPanel>
                <TextBlock Text="{Binding}"></TextBlock>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <ItemsControl  ItemTemplate="{StaticResource myTemplate}" ItemsSource="{Binding List}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>


</Window>

In code behind I have this to simulate:

using System.Collections.Generic;
using System.Windows;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.List = new List<string> { "Quien", "vie", "ne", "en", "su", "nom", "bre" };
            InitializeComponent();
            this.DataContext = this;
        }

        public List<string> List { get; set; }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {


        }
    }
}

Upvotes: 1

Related Questions