2012-04-04 18 views
1

我想列出一個對象數組並將這些項目水平地均勻分佈。如果它不是一個數據綁定數組,我只需創建一個具有正確列數的網格並將每個項目分配給一列。問題是我不知道如何用數據綁定列表控件做到這一點。如何水平分佈WPF中的對象數組?

作爲一種廉價的替代我的項目被列水平使用一個StackPanel作爲ItemsPanel爲一個ItemsControl這樣的:

<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保證金?這不是絕對的價值嗎?如果是這樣,不會使用絕對值需要調整,這取決於數組中有多少項以及窗口有多大? – 2012-04-04 16:06:30

+1

啊,好的。是的,0 0 8 0的邊際是任意的,因爲我以爲你只是想有一些空間分隔你的物品。要根據Item容器的數量來定位Item容器,可以看看是否有方法將UniformGrid用作ItemsPanelTemplate。我沒有嘗試過,但我想你需要將它限制爲1行,並找到一些方法將列數更改爲ItemsCollection中的項目數。如果你喜歡,我會在一段時間內看看我是否可以測試這個。 – CodeWarrior 2012-04-04 16:12:05

+0

謝謝你,這是我試圖解決的問題的核心 – 2012-04-04 16:21:24

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>