.net ListView内的WPF网格和标题显示的大小不一致

jm2pwxwz  于 2023-02-14  发布在  .NET
关注(0)|答案(1)|浏览(133)

我在列表视图项目模板和标题网格中有网格。这是该部分的代码。
'

<!--Products listview headers-->
            <Grid HorizontalAlignment="Stretch">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"  />
                    <ColumnDefinition Width="150" />
                    <ColumnDefinition Width="150" />
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0"
                           Text="Order No" 
                           VerticalAlignment="Center"
                           HorizontalAlignment="Left"/>
                <TextBlock Grid.Column="1"
                           Text="Customer" 
                           HorizontalAlignment="Left"
                           VerticalAlignment="Center"/>
                <TextBlock Grid.Column="2"
                           Text="Total"                                             
                           VerticalAlignment="Center"
                           HorizontalAlignment="Right"/>
            </Grid>

            <Border Style="{StaticResource HRule}" />

            <!--Products listview-->

            <ListView x:Name="listView" 
                      ItemsSource="{Binding SalesOrders}" 
                      SelectedItem="{Binding SelectedSalesOrder, Mode=TwoWay}"
                      SelectionMode="Single"
                      Padding="0"
                      Margin="0">
                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                        <Style.Triggers>
                            <Trigger Property="IsSelected" Value="True" >
                                <Setter Property="FontWeight" Value="Bold" />
                                <Setter Property="Background" Value="Transparent" />
                                <Setter Property="Foreground" Value="Black" />
                            </Trigger>
                        </Style.Triggers>
                        <Style.Resources>
                            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
                            <!-- Background of selected item when focussed -->
                            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightCyan"/>
                            <!-- Background of selected item when not focussed -->
                            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="LightGray" />
                        </Style.Resources>
                    </Style>
                </ListView.ItemContainerStyle>
                <ListView.ItemTemplate>
                    <DataTemplate DataType="{x:Type entities:SalesOrder}">
                        <Grid HorizontalAlignment="Stretch">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"  />
                                <ColumnDefinition Width="150" />
                                <ColumnDefinition Width="150" />
                            </Grid.ColumnDefinitions>
                            <TextBlock Grid.Column="0"
                                       Text="{Binding No, StringFormat={StaticResource serialNoFormat}}" 
                                       VerticalAlignment="Center"
                                       HorizontalAlignment="Left"/>
                            <TextBlock Grid.Column="1"
                                       Text="{Binding Customer.Name}" 
                                       HorizontalAlignment="Left"
                                       VerticalAlignment="Center"/>
                            <TextBlock Grid.Column="2"
                                       Text="{Binding Total, StringFormat={StaticResource currencyFormat}}"                                             
                                       VerticalAlignment="Center"
                                       HorizontalAlignment="Right"/>
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackPanel>`

当前标题和行中的列未对齐。当前的外观如下所示。

我找不到错位的原因。
我试着将列表视图的填充和边距设置为0,但没有效果。我试着为网格设置共享大小范围,但也没有任何运气。

jgovgodb

jgovgodb1#

不要使用网格和列表视图,而是将GridView与ListView一起使用。
因此:

<ListView>
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn Header="Order No">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock
                                   Text="{Binding No, StringFormat={StaticResource serialNoFormat}}" 
                                   VerticalAlignment="Center"
                                   HorizontalAlignment="Left"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Customer">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock 
                                   Text="{Binding Customer.Name}" 
                                   HorizontalAlignment="Left"
                                   VerticalAlignment="Center"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Total">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock
                                   Text="{Binding Total, StringFormat={StaticResource currencyFormat}}"                                             
                                   VerticalAlignment="Center"
                                   HorizontalAlignment="Right"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>

试一试,看看它是否做了你需要它做的事情。

相关问题