在WPF中填充ListView项目

时间:2016-09-25 08:53:49

标签: c# wpf xaml

它似乎适合所有人,但不适合我。我希望在窗口较大时水平填充ListView项目,然后使用项目堆叠面板。我有以下.xaml

    <ListView x:Name="comboBox" ItemsSource="{Binding ArticleCategories}" SelectedItem="{Binding SelectedArticleCategory}" HorizontalAlignment="Stretch" Margin="0,0,0,0" Background="Azure" VerticalAlignment="Top">
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" ></Setter>
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.ItemTemplate>
            <DataTemplate>
                <Button Content="abc" Width="Auto"></Button>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

这就是我所拥有的 enter image description here

1 个答案:

答案 0 :(得分:1)

您可以将一行UniformGrid用作ItemsPanel

<ListView ...>
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Rows="1"/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.ItemTemplate>
        <DataTemplate>
            <Button Content="abc"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>