繁体   English   中英

在WPF中使StackPanel方向水平

[英]Make a StackPanel Orientation Horizontal in WPF

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

当我运行该应用程序时,即使将内部StackPanel的Orientation设置为Horizontal我的所有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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM