使用网格作为ItemsHost [英] Using a Grid as an ItemsHost

查看:96
本文介绍了使用网格作为ItemsHost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Grid作为ItemsHost,但是没有任何项目出现在其绑定(列,行)位置.我该如何进行这项工作?举一个简单的例子:

I would like to use a Grid as an ItemsHost but none of the items appear in their bound (column, row) positions. How can I make this work? As a simple example:

XAML

<ItemsControl ItemsSource="{Binding DataSet}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Grid.Column="{Binding Col}" Grid.Row="{Binding Row}" Text="{Binding Text}"   />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.Style>
        <Style TargetType="{x:Type ItemsControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ItemsControl}">
                        <Grid HorizontalAlignment="Stretch" IsItemsHost="True">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition />
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition />
                                <RowDefinition />
                            </Grid.RowDefinitions>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ItemsControl.Style>
</ItemsControl>

隐藏代码

Class Window1 

    Private myTestData As TestData

    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        myTestData = New TestData()
        Me.DataContext = myTestData
    End Sub

End Class

Class TestData

    Private myDataSet As List(Of DataPoint)
    Public Property DataSet() As List(Of DataPoint)
        Get
            Return myDataSet
        End Get
        Set(ByVal value As List(Of DataPoint))
            myDataSet = value
        End Set
    End Property

    Public Sub New()
        Me.DataSet = New List(Of DataPoint)
        For x As Integer = 0 To 1
            For y As Integer = 0 To 1
                Me.DataSet.Add(New DataPoint(x, y, "Column " + x.ToString + ", Row " + y.ToString))
            Next
        Next
    End Sub

End Class

Class DataPoint

    Private myRow As Integer
    Public Property Row() As Integer
        Get
            Return myRow
        End Get
        Set(ByVal value As Integer)
            myRow = value
        End Set
    End Property

    Private myCol As Integer
    Public Property Col() As Integer
        Get
            Return myCol
        End Get
        Set(ByVal value As Integer)
            myCol = value
        End Set
    End Property

    Private myText As String
    Public Property Text() As String
        Get
            Return myText
        End Get
        Set(ByVal value As String)
            myText = value
        End Set
    End Property

    Public Sub New(ByVal x As Integer, ByVal y As Integer, ByVal name As String)
        Me.Row = y
        Me.Col = x
        Me.Text = name
    End Sub

End Class

推荐答案

由于使用的是ItemsControl,因此会为每个项目生成一个容器.该容器(普通的ItemsControlContentPresenter实例)包装TextBlock,并且是Grid的直接子代.因此,Grid甚至不会在TextBlock上看到ColumnRow属性,因为它正在查看容器.

Because you're using an ItemsControl, a container is generated for each item. That container (an instance of ContentPresenter for a plain old ItemsControl) wraps the TextBlock, and is a direct child of the Grid. Therefore, the Grid never even sees the Column and Row properties on the TextBlock because it's looking instead at the container.

您可以通过设置ItemContainerStyle来绑定容器的适当属性,以解决此问题:

You can solve this by setting an ItemContainerStyle that binds the appropriate properties for the container:

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

这篇关于使用网格作为ItemsHost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆