我想列出一个对象数组,并让这些项目水平均匀展开。如果它不是数据绑定数组,我只需要使用正确数量的列创建一个网格,并将每个项目分配给一列。问题是我不知道如何使用数据绑定列表控件。
作为一种廉价的替代方案,我使用stackpanel水平列出项目作为ItemsControl的ItemsPanel,如下所示:
<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 :(得分:3)
这是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>
答案 1 :(得分: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>