2009-03-27 9 views
2

GridをItemsHostとして使用したいのですが、項目のいずれもバインド(列、行)位置に表示されません。どうすればこの作品を作れますか?簡単な例として:グリッドを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> 

分離コード

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 

答えて

3

あなたがItemsControlを使用しているので、コンテナは、各項目ごとに生成されます。そのコンテナ(普通の古いItemsControlの場合はContentPresenterのインスタンス)はTextBlockをラップし、Gridの直属の子です。そのため、GridTextBlockのコンテナではなく、Columnとのプロパティを表示しません。

あなたは、コンテナのための適切なプロパティをバインドしItemContainerStyleを設定することによってこの問題を解決することができます

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

あなたは私のヒーローになりました。 –

1

可能な回避策を:あなたはUniformGridを使用する場合は、行と列を指定する必要はないかもしれません。

関連する問題