Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Data binding: How to bind items in ItemsCollection to Grid.Row and Grid.Column?

Tags:

binding

wpf

I have a class:

public class TempClass
{
    public int Row
    {
        get { return row; }
        set { row = value; }
    }
    public int Column
    {
        get { return column; }
        set { column = value; }
    }
    public string Value
    {
        get { return this.value; }
        set { this.value = value; }
    }

    int row;
    int column;
    string value;

    public TempClass(int row, int column, string value)
    {
        this.row = row;
        this.column = column;
        this.value = value;
    }
}

I have created a List<TempClass> DummyCollection and I have bound it to an ItemsControl. The ItemsControl uses a UniformGrid panel for ItemsPanel property. Here is the XAML code:

<ItemsControl ItemsSource="{Binding Path=DummyCollection}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Rows="3" Columns="5" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate DataType="TempClass">
            <Border>
                <TextBlock Text="{Binding}" />
            </Border>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

I want to be able to bind TempClass items to the particular cell in the ItemsControl, i.e. I want to set the Grid.Row and Grid.Column properties of the item container to match Row and Column properties of the TempClass items. How do I achieve that? Thanks.

like image 619
Boris Avatar asked Mar 27 '11 18:03

Boris


1 Answers

<ItemsControl>
    <ItemsControl.ItemContainerStyle>
        <Style>
            <Setter Property="Grid.Row" Value="{Binding Row}"/>
            <Setter Property="Grid.Column" Value="{Binding Column}"/>
        </Style>
    </ItemsControl.ItemContainerStyle>
    ...
</ItemsControl>
like image 107
Kent Boogaart Avatar answered Oct 26 '22 19:10

Kent Boogaart