2

我想列出一组对象,并让这些项目水平均匀分布。如果它不是一个数据绑定数组,我只需创建一个具有正确列数的网格并将每个项目分配给一列。问题是我不知道如何使用数据绑定列表控件来做到这一点。

作为一种廉价的替代方案,我使用堆栈面板作为 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>

有没有办法让它们均匀分布?

4

2 回答 2

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>
于 2012-04-04T20:41:57.013 回答
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>
于 2012-04-04T15:54:36.097 回答