我通过单击列标题对 GridView 进行排序。但是,当我向 gridview 添加更多项目并且它变得可滚动时,第一次单击列标题不会对项目进行排序。相反,它向上移动 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>

最佳答案

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

这应该可以防止 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;
}

关于c# - 单击 gridview 列标题整个 gridview 试图覆盖窗口,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9588729/

10-16 10:13