2012-04-04 5 views
1

オブジェクトの配列をリストし、アイテムを水平に均等に広げたいと考えています。データバインドされた配列でない場合は、適切な数の列でグリッドを作成し、各項目を列に割り当てます。問題は、私はデータバインドされたリストコントロールでそれを行う方法がわかりません。WPFでオブジェクトの配列を水平に配布する方法は?

安い代替として、私は水平に、このようなのItemsControlのためのItemsPanelとしてのStackPanelを使用してリストされているアイテムを持っている:

<ItemsControl ItemsSource="{Binding Path=ValveSettings}" Grid.Row="0"> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <StackPanel Orientation="Horizontal" /> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <Grid> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="auto" /> 
         <RowDefinition /> 
         <RowDefinition Height="auto" /> 
        </Grid.RowDefinitions> 
        <Label Content="{Binding Path=Name}" Grid.Row="0" /> 
        <ScrollBar Orientation="Vertical" Grid.Column="0" Grid.Row="1" Minimum="0" Maximum="100000" Value="{Binding Path=DelayInMicroseconds}" SmallChange="100" LargeChange="1000" /> 
        <TextBox Text="{Binding Path=DelayInMicroseconds}" Grid.Row="2" /> 
       </Grid> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 

は、私は均等に広がることができます方法はありますか?

答えて

0

私は一般に、ItemTemplateに余白を付けてリストアイテムを広げました。マージンを非開始側に置く(つまり、リストが左から入力された場合は、マージンを右に置く)。このようにして、連続する各アイテムは、前のアイテムからのx個のピクセルに配置されます。

上記の例では、Grid要素に余白を追加しました。

 <DataTemplate> 
      <Grid Margin="0 0 8 0"> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="auto" /> 
        <RowDefinition /> 
        <RowDefinition Height="auto" /> 
       </Grid.RowDefinitions> 
       <Label Content="{Binding Path=Name}" Grid.Row="0" /> 
       <ScrollBar Orientation="Vertical" Grid.Column="0" Grid.Row="1" Minimum="0" Maximum="100000" Value="{Binding Path=DelayInMicroseconds}" SmallChange="100" LargeChange="1000" /> 
       <TextBox Text="{Binding Path=DelayInMicroseconds}" Grid.Row="2" /> 
      </Grid> 
     </DataTemplate> 
+0

どのように8のマージンを選ぶのですか?この絶対値ではありませんか?絶対値を使用しない場合、配列内のアイテムの数とウィンドウの大きさに応じて調整が必要ですか? –

+1

ああ、OK。はい、私はあなたのアイテムを分けるスペースがあると思っていたので0 0 8 0の余白は任意です。アイテムコンテナの数に基づいてアイテムコンテナを配置するには、ItemsPanelTemplateとしてUniformGridを使用する方法があるかどうかを確認する必要があります。私はこれを試していませんが、1行に制限し、ItemsCollectionの項目数に列数を変更する方法を見つける必要があると思います。あなたが好きなら、これをしばらく試すことができるかどうかがわかります。 – CodeWarrior

+0

ありがとう、それは私が解決しようとしている問題の中心です –

1

これは素晴らしい作品のCodeWarriorとコメントの結果である:

<ItemsControl ItemsSource="{Binding Path=Settings.Valves}" Grid.Row="0"> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <UniformGrid Rows="1" /> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <Grid> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="auto" /> 
         <RowDefinition /> 
         <RowDefinition Height="auto" /> 
        </Grid.RowDefinitions> 
        <TextBlock Text="{Binding Path=Name}" Grid.Row="0" TextAlignment="Center" /> 
        <ScrollBar Orientation="Vertical" Grid.Column="0" Grid.Row="1" Minimum="0" Maximum="100000" Value="{Binding Path=DelayInMicroseconds}" SmallChange="100" LargeChange="1000" /> 
        <TextBox Text="{Binding Path=DelayInMicroseconds}" Grid.Row="2" TextAlignment="Center" /> 
       </Grid> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl>