Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: rows and columns in an ItemsControl

I've tried putting children of both a ListView and an ItemsControl in rows and columns, by setting a grid with RowDefinitions and ColumnDefinitions as the ItemsPanel property.

However the child control always aligns to Row 1 and Column 1, when I put

<ItemsControl>

   <ItemsControl.ItemsPanel>
      <!-- Grid with rows & columns ... -->
   </ItemsControl.ItemsPanel>

   <ItemsControl.ItemTemplate>
      <DataTemplate>
         <TextBlock Grid.Row="4" Grid.Column="2" ... />
      </DataTemplate>
   </ItemsControl.ItemTemplate>

</ItemsControl>

How can I make this work? Thank you.

like image 496
Fabi Avatar asked Dec 05 '25 04:12

Fabi


2 Answers

You should set the Grid.Row and Grid.Column attached properties of the container that wraps the root element of the ItemTemplate:

<ItemsControl x:Name="iccc">

    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock>cell 2:2...</TextBlock>
        </DataTemplate>
    </ItemsControl.ItemTemplate>

    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="Grid.Row" Value="1" />
            <Setter Property="Grid.Column" Value="1" />
        </Style>
    </ItemsControl.ItemContainerStyle>

</ItemsControl>
like image 173
mm8 Avatar answered Dec 07 '25 20:12

mm8


Set the ´Grid´ in the ´ItemTemplate´ rather than the ´ItemPanel´. See example here: http://www.wpf-tutorial.com/list-controls/itemscontrol/


public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        PersonCollection = new ObservableCollection<Person>()
        {
            new Person() { FirstName = "John", LastName = "Doe" },
            new Person() { FirstName = "Richard", LastName = "Bryson" },
            new Person() { FirstName = "Bill", LastName = "Gates" },
            new Person() { FirstName = "Adam", LastName = "Sandler" }
        };
        itemsControl.ItemsSource = PersonCollection;
    }
    public ObservableCollection<Person> PersonCollection { get; set; }
}

<ItemsControl x:Name="itemsControl">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid Margin="0,0,0,5">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{Binding Path=FirstName}" />
                <TextBlock Grid.Column="1" Text="{Binding Path=LastName}" />
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
like image 41
Oystein Avatar answered Dec 07 '25 19:12

Oystein