문제

그리드를 ItemShost로 사용하고 싶지만 항목 중 어느 것도 바운드 (열, 행) 위치에 나타나지 않습니다. 이 작업을 어떻게 만들 수 있습니까? 간단한 예로 :

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>

CodeBehind

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, 컨테이너는 각 항목에 대해 생성됩니다. 그 컨테이너 (인스턴스 ContentPresenter 평범한 늙음 ItemsControl) 랩 TextBlock, 그리고 직접적인 자녀입니다 Grid. 따라서, Grid 결코 보지 못합니다 Column 그리고 Row 속성 TextBlock 대신 컨테이너를보고 있기 때문입니다.

설정하여 이것을 해결할 수 있습니다 ItemContainerStyle 컨테이너의 적절한 속성을 바인딩합니다.

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

다른 팁

가능한 해결 방법 : 사용하는 경우 유니폼 그리드, 행과 열을 지정할 필요가 없습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top