WPF数据绑定:如何将ItemsCollection中的项绑定到Grid.Row和Grid.Column?

时间:2011-03-27 18:39:54

标签: wpf binding

我有一个班级:

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;
    }
}

我创建了List<TempClass> DummyCollection并将其绑定到ItemsControlItemsControl使用UniformGrid面板作为ItemsPanel属性。这是XAML代码:

<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>

我希望能够将TempClass项绑定到ItemsControl中的特定单元格,即我想设置项容器的Grid.RowGrid.Column属性匹配Row项的ColumnTempClass属性。我如何实现这一目标?谢谢。

1 个答案:

答案 0 :(得分:7)

<ItemsControl>
    <ItemsControl.ItemContainerStyle>
        <Style>
            <Setter Property="Grid.Row" Value="{Binding Row}"/>
            <Setter Property="Grid.Column" Value="{Binding Column}"/>
        </Style>
    </ItemsControl.ItemContainerStyle>
    ...
</ItemsControl>