wpf ListView 填充父、屏幕或其他任何东西的宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30591174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:10:48  来源:igfitidea点击:

ListView fill the width of parent, screen or whatever

wpflayoutwindows-phone-8.1

提问by Andreas777

I am trying to display some data from an Observable Collection to a ListView. Binding is working fine. My only problem I cannot fix is the layout of the ListView.I use a Grid with 2 rows to split my View. The ListView is one the second row. I only try to use * for setting Columns and Rows as suggested in most sources. The code for the ListView is this.

我正在尝试将 Observable 集合中的一些数据显示到 ListView。绑定工作正常。我唯一无法解决的问题是 ListView 的布局。我使用带有 2 行的 Grid 来拆分我的视图。ListView 是第二行之一。我只尝试使用 * 来设置大多数来源中建议的列和行。ListView 的代码是这样的。

<Grid Grid.Row="1" Margin="10,0,10,0" HorizontalAlignment="Stretch">
    <ListView ItemsSource="{Binding Collection}" HorizontalAlignment="Stretch">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid HorizontalAlignment="Stretch">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0" HorizontalAlignment="Center" Text="{Binding Period}" Foreground="{StaticResource ColorBrush}" FontFamily="{StaticResource Family}" FontSize="20"/>
                    <TextBlock Grid.Column="1" HorizontalAlignment="Center" Text="{Binding Currency}" Foreground="{StaticResource ColorBrush}" FontFamily="{StaticResource Family}" FontSize="20"/>
                    <TextBlock Grid.Column="2" HorizontalAlignment="Center" Text="{Binding Date}" Foreground="{StaticResource ColorBrush}" FontSize="20" FontFamily="{StaticResource Family}"/>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

When I use HorizontalAlignment="Stretch" for the Grid that contains the ListView and the ListView itself the data is displayed on the left side without getting the whole width of parent View. When I explicitly set Width to some value (i.e.380) it displays OK.

当我对包含 ListView 和 ListView 本身的 Grid 使用 Horizo​​ntalAlignment="Stretch" 时,数据显示在左侧,而没有获得父视图的整个宽度。当我将 Width 显式设置为某个值 (ie380) 时,它显示 OK。

<Grid Grid.Row="1" Margin="10,0,10,0">
    <ListView ItemsSource="{Binding Collection}" Width="380">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid Width="380">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0" HorizontalAlignment="Center" Text="{Binding Period}" Foreground="{StaticResource ColorBrush}" FontFamily="{StaticResource Family}" FontSize="20"/>
                    <TextBlock Grid.Column="1" HorizontalAlignment="Center" Text="{Binding ValueInCurrency}" Foreground="{StaticResource ColorBrush}" FontFamily="{StaticResource Family}" FontSize="20"/>
                    <TextBlock Grid.Column="2" HorizontalAlignment="Center" Text="{Binding Date}" Foreground="{StaticResource ColorBrush}" FontSize="20" FontFamily="{StaticResource Family}"/>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

But I want to know a better way to not use implicit values, but *.

但我想知道一种更好的方法来不使用隐式值,而是使用 *.

回答by alfah

Try this. Add the following jus after </ListView.ItemTemplate>

尝试这个。在后面添加以下内容</ListView.ItemTemplate>

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    </Style>
</ListView.ItemContainerStyle>