c# - 在 WPF 中使 StackPanel 方向水平

标签 c# wpf xaml

我在 View 中有这个 xaml 代码

<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 绑定(bind)到我的ViewModel 中的List(如代码所示)

当我运行应用程序时,即使我已将内部 StackPanel 的 Orientation 设置为 Horizo​​ntal,我的所有 TextBlocks 都会垂直显示。

最佳答案

要更改 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 有 3 个有趣的属性来定义它的呈现方式:

这是使用所有这些属性的代码:

<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>

关于c# - 在 WPF 中使 StackPanel 方向水平,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17908474/

相关文章:

c# - Xamarin 表单切换按钮

c# - 将动态创建的菜单项从 Button.ContextMenu 绑定(bind)到 ICommand

c# - "multi-threadedly"在 async/await 模式下实际上执行了什么代码?

c# - 使用 EF 6 在同一个数据库中使用多个 dbcontext

c# - XML 节点为空时丢失

c# - Azure Function 正常运行一段时间后无法加载程序集

wpf - 单击组名称时 CollectionViewSource 取消选择 selectedItem

wpf - 在所有其他列都采用所需宽度后,如何设置网格的第一列以适合可用空间?

c# - 将动态类型转换为列表

c# - ListBox VirtualizingStackPanel.VirtualizationMode ="Recycling"在 Windows Phone 7.5 中不起作用