【问题标题】:WPF DataGrid disable auto scrollWPF DataGrid 禁用自动滚动
【发布时间】:2019-06-22 16:33:51
【问题描述】:

基本上,我有一个应用程序可以帮助我对我的照片和电影收藏进行分类/分类。这真的很简单,我有一个DataGrid,其中列出了特定文件夹中的所有文件。有两列:KeepFilename。我将使用箭头向下此 DataGrid 并按空格是否要保留文件(第一列是bool,所以当我按空格时,复选框被选中)。因此,无需单击任何内容,它看起来像这样:

但是当我点击文件名时(当我点击同一行但Keep 列时,它不会滚动,但与文件名列相比它要小得多,所以我最终总是点击文件名列行的一部分)并且它太长了它水平滚动,像这样:

问题是现在我看不到Keep 列,所以我必须手动向后滚动,看看我是否标记了文件。所以为了解决这个问题,我在 SO 上看到了很多建议编辑 MainWindow 的 XAML 部分的答案。问题是,这是我的 XAML 文件:

<Controls:MetroWindow x:Class="FileOrganiser.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        xmlns:local="clr-namespace:FileOrganiser"
        mc:Ignorable="d"
        Title="File Organiser" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*" />
            <RowDefinition Height="8*" />
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>
        <MediaElement x:Name="Media" Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" Margin="5"/>
        <DataGrid x:Name="FilesList" Grid.Column="0" Grid.Row="1" Grid.RowSpan="2" HorizontalAlignment="Center"
                  SelectionChanged="FilesList_OnSelectionChanged">
        </DataGrid>
        <Button x:Name="ButtonSOrt" Grid.Column="1" Grid.Row="2"></Button>
    </Grid>
</Controls:MetroWindow>

重要的是,我自己不定义列,这就是我填充数据网格的方式:

public MainWindow()
{
    InitializeComponent();
    while (true)
    {
        RootDir = FileUtils.SelectRootFolder();
        if (RootDir == string.Empty) MessageBox.Show("Select a root folder!");
        else break;
    }
    files = Directory.GetFiles(RootDir);
    var videos = files.Select(file => new Video(Path.GetFileName(file), false)).ToList();
    FilesList.ItemsSource = videos;
}

所以我通过更改代码中的ItemSource 来做到这一点。如果我自己定义列并像这样更改ItemSource,我将有 4 列而不是 2 列。那么当我以这种方式实现时,有没有办法防止这种自动滚动?

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    决定将此作为另一个答案,因为这应该是首选方式,因为它可以让您更好地控制布局:

    把它放在你的 XAML 中:

    <DataGrid x:Name="FilesList" HorizontalAlignment="Center" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridCheckBoxColumn Binding="{Binding Keep}" Width="25"/>
            <DataGridTextColumn Binding="{Binding Filename}" Width="*"/>
        </DataGrid.Columns>
    </DataGrid>
    

    后面的代码应该不需要更改。

    【讨论】:

      【解决方案2】:

      Datagrid 应该有一个附加属性: https://docs.microsoft.com/de-de/dotnet/api/system.windows.controls.scrollviewer.horizontalscrollbarvisibility?view=netframework-4.8

      将此设置为“禁用”:

      Scrollviewer.HorizontalScrollBarVisibility="Disabled"
      

      【讨论】:

      • 隐藏滚动条不会改变鼠标点击后滚动的事实。滚动条不存在,但它仍然滚动
      • 啊,我的错。当然,您必须将其设置为禁用;)
      • 另外一件事:你也可以将DataGrid的AutoGenerateColumns属性设置为false,然后自己定义列,最后不会有4列。
      猜你喜欢
      • 2017-07-12
      • 2012-11-17
      • 2014-06-24
      • 2011-10-06
      • 2013-06-23
      • 1970-01-01
      • 2016-07-24
      • 1970-01-01
      • 2011-02-18
      相关资源
      最近更新 更多