在 ListView ItemTemplate 中制作网格填充 [英] Making a grid in a ListView ItemTemplate fill

查看:15
本文介绍了在 ListView ItemTemplate 中制作网格填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListView,它有一个基于网格的数据模板.xml如下:

I have a ListView which has a data template based on a grid. The xaml is as follows:

<ListView ItemsSource="{Binding SomeItemSource}" HorizontalAlignment="Stretch" Height="281">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid HorizontalAlignment="Stretch" Margin="3" Width="Auto"> 
                <Grid.RowDefinitions>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="70"></ColumnDefinition>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <!-- Grid content here -->
                <TextBlock Text="SomeTextbox" 
                           Margin="5,5,0,0"/>

                <TextBox Grid.Column="1" 
                         Margin="0,5,0,0"
                         Text="{Binding SomeProperty, Mode=TwoWay}"
                         HorizontalAlignment="Left"
                         Width="90"/>

                <TextBlock Text="AnotherText" 
                           Grid.Column="0" 
                           Grid.Row="1"
                           Margin="5,5,0,0"/>

                <TextBox Grid.Column="1" Grid.Row="1"
                         Margin="0,5,0,0"
                         Text="{Binding AnotherProperty, Mode=TwoWay}" 
                         HorizontalAlignment="Stretch"
                         Width="300"/>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

我想要的是让网格填充(延伸)父 ListView 的整个水平宽度,但是它当前包裹到其内容总和的宽度.我怎样才能实现我想要的行为?

What I want, is for the grid to fill (stretch over) the entire horizontal width of the parent ListView, however it currently wraps to the width of the sum of its contents. How can I achieve the behavior I want?

推荐答案

您需要将 ListViewItem.Horizo​​ntalContentAlignment 设置为 Stretch.尝试将此添加到您的 ListView 定义中:

You need to set ListViewItem.HorizontalContentAlignment to Stretch. Try adding this into your ListView definition:

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    </Style>
</ListView.ItemContainerStyle>

这篇关于在 ListView ItemTemplate 中制作网格填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆