单击gridview列标题,整个gridview试图覆盖窗口

本文关键字:gridview 覆盖 窗口 标题 单击 整个 | 更新日期: 2023-09-27 18:26:49

我正在通过单击列标题对GridView进行排序。然而,当我向网格视图添加更多项目并使其可滚动时,第一次单击列标题不会对项目进行排序。相反,它会向上移动网格视图,并尝试显示可用空间中的所有项目。

我是否必须设置一些属性来防止这种情况发生?以下是GridView:的代码

<Window x:Class="AutomatedExitStrategy.Presentation.View.AutomatedExitStrategyShell"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Automated Exit Strategy System" Height="480" 
    Width="2125">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
    <Grid Opacity="1">
    <Grid.RowDefinitions>
        <RowDefinition Height="0.20*"/>
        <RowDefinition Height="0.80*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
        <ListView
            Grid.Row="1" 
            Margin="0,2,0,0" 
            IsSynchronizedWithCurrentItem="True" 
            ItemsSource="{Binding GlobalRowsCollection}"
            AlternationCount="2">
            <ListView.View>
                <GridView>
                    <GridViewColumn
                        Width="25">
                        <GridViewColumnHeader Command="{Binding SortList}" CommandParameter="IsEnabled"></GridViewColumnHeader>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox Margin="0"
                                          VerticalAlignment="Center"
                                          HorizontalAlignment="Left"
                                          IsChecked="{Binding IsChecked}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn 
                        Width="Auto">
                        <GridViewColumnHeader Command="{Binding SortList}" CommandParameter="InputViewModel.Group">Grp</GridViewColumnHeader>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Grid>
                                    <TextBlock Text="{Binding InputViewModel.Group}" 
                                               Style="{StaticResource GridBlockStyle}"
                                               HorizontalAlignment="Center"/>
                                </Grid>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

单击gridview列标题,整个gridview试图覆盖窗口

您应该订阅ListView的RequestBringIntoView事件并将其标记为Handled。

这样可以防止ScrollViewer在单击时向下滚动以尝试显示整个ListView。因此,它应该允许MouseDown事件继续进行并触发排序

<ListView Grid.Row="1" 
          Margin="0,2,0,0" 
          IsSynchronizedWithCurrentItem="True" 
          ItemsSource="{Binding GlobalRowsCollection}"
          AlternationCount="2"
          RequestBringIntoView="ListView_RequestBringIntoView">
    ...
</ListView>

在背后的代码上。。。

public void ListView_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
{
    e.Handled = true;
}