在WPF中将StackPanel的方向设置为水平方向

11

我有一个 xaml 代码在一个 View 中。

<StackPanel>
    <Button Content="I am IRON" />
    <ListView ItemsSource="{Binding Path=MeasuringDeviceCommunicators}">
        <ListView.ItemTemplate>              
            <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Name}"/>
            </StackPanel>
        </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackPanel>

ListViewItemSource绑定到我的ViewModel中的List(如代码所示)。

当我运行应用程序时,尽管我已将内部StackPanelOrientation设置为Horizontal,但所有的TextBlocks仍然垂直显示。


尝试使用WrapPanel而不是内部使用StackPanel。 - Smaug
@RameshMuthiah 它没起作用 :( 顺便说一下,我对WPF和XAML真的很新。 - Mehrdad Kamelzadeh
我希望@Cédric Bignon的解决方案能够帮到你。请尝试一下。 - Smaug
1个回答

17

要更改 ListView 的布局,请使用 ItemsControl.ItemsPanel 属性:

<StackPanel>
    <Button Content="I am IRON" />
    <ListView ItemsSource="{Binding Path=MeasuringDeviceCommunicators}"  >
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <!-- Here is the panel that will contain the items -->
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
        <ListView.ItemTemplate>
            <DataTemplate>
                <!-- Your item Template is here -->
                <TextBlock Text="{Binding Path=Name}"/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackPanel>

如果您需要展示大量项目,也许您想使用VirtualizingStackPanel代替StackPanel,这可能会提高性能。

更新

如果您想在堆栈面板的每个项目中添加一个列表,可以通过修改ItemTemplate(表示如何显示每个项目)来实现。

例如:

<ListView.ItemTemplate>
    <DataTemplate>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="8"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <TextBlock Text="{Binding Path=Name}"/>

             <!-- Displays the tags (or whatever you want) -->
            <ListView Grid.Column="2" ItemsSource="{Binding Tags}"/>
        <Grid>
    </DataTemplate>
</ListView.ItemTemplate>

总之,ListView 有三个有趣的属性可以定义它的呈现方式:

下面是一个使用所有这些属性的代码:

<ListView>
    <ListView.Items>
        <Button Content="Button 1"/>
        <Button Content="Button 2"/>
        <Button Content="Button 3"/>
        <Button Content="Button 4"/>
    </ListView.Items>

    <!-- The layout of the list (position and size of the elements -->
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <!-- StackPanel means : the elements are rendered in stack, either horizontally or vertically (the way it is rendered in StackPanel is defined in code -->
            <StackPanel Orientation="Vertical" Background="LightCoral"/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>

    <!-- How I want the list to look like? -->
    <ListView.Template>
        <ControlTemplate>
            <!-- A blue background with a green border -->
            <Border Background="LightBlue" BorderBrush="DarkGreen" BorderThickness="5">
                <!-- ItemsPresenter "represents" the ItemsPanel defined above -->
                <ItemsPresenter HorizontalAlignment="Right" />
            </Border>
        </ControlTemplate>
    </ListView.Template>

    <!-- How I want each item to look like? -->
    <ListView.ItemTemplate>
        <DataTemplate>
            <!-- A "This is an item:" label followed by the item itself --> 
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="8"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <TextBlock Grid.Column="0" Text="This is an item : "/>
                <ContentPresenter Grid.Column="2" Content="{Binding}"/>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView> 

请注意,这部分内容:

<ListView.ItemsPanel>
    <ItemsPanelTemplate>
        <StackPanel Orientation="Vertical" Background="LightCoral"/>
    </ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.Template>
    <ControlTemplate>
        <Border Background="LightBlue" BorderBrush="DarkGreen" BorderThickness="5">
            <ItemsPresenter HorizontalAlignment="Right" />
        </Border>
    </ControlTemplate>
</ListView.Template>

等价于:

<ListView.Template>
    <ControlTemplate>
        <Border Background="LightBlue" BorderBrush="DarkGreen" BorderThickness="5">
            <StackPanel Orientation="Vertical" Background="LightCoral"
                        HorizontalAlignment="Right"
                        IsItemsHost="True"/>
        </Border>
    </ControlTemplate>
</ListView.Template>

@MarkHomer 我在你发表评论几秒钟前更新了我的帖子。 ;) - Cédric Bignon
哇,它起作用了。谢谢。但我很难过,因为我不知道如何学习这些东西。对于像我这样曾经使用过“Winforms”的人来说,这有点奇怪。@CédricBignon,你建议学习WPF的书籍或参考资料是什么? - Mehrdad Kamelzadeh
除了Cédric发布的内容(我完全同意),我建议在ItemsPanelTemplate中的(Stack)Panel中设置IsItemsHost =“true”。 - Mario Vernari
如果 StackPanelListView.ControlTemplate 中定义,则需要 IsItemsHost = "true"。如果在 ItemsPanel 中设置,则不需要。 - Cédric Bignon
我必须承认,在ItemsPanelTemplate中的uselessly设置我并不知道:我总是为目标面板进行设置。谢谢Cèdric!@Mehrad:这需要项目生成器知道要将项目容器附加到哪里。内部引擎寻找此属性以查找目标面板。 - Mario Vernari
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接