[英]WPF DataGrid disable auto scroll
Basically I have an application to help me sort/catalogue my collection of photos and movies. 基本上,我有一个应用程序可以帮助我对照片和电影的收藏进行分类/分类。 It's really simple, I have a DataGrid
which has all the files in a specific folder listed in it. 真的很简单,我有一个DataGrid
,其中列出了特定文件夹中的所有文件。 There are two columns: Keep
and Filename
. 有两列: Keep
和Filename
。 I would go down this DataGrid using arrows and press space whether I want to keep the file (the first column is bool
, so when I press space, the checkbox is being checked). 我将使用箭头浏览此DataGrid,然后按空格键是否要保留文件(第一列是bool
,所以当我按空格键时,该复选框处于选中状态)。 So without clicking anything it looks like this: 因此,无需单击任何内容,它看起来像这样:
But when I click on the filename (when I click on the same row, but Keep
column, it doesn't scroll, but it's so much smaller compared to filename column, so I end up always clicking the filename column part of the row) and it's too long it scrolls horizontally, like this: 但是,当我单击文件名时(当我单击同一行但Keep
列时,它不会滚动,但与文件名列相比要小得多,所以最终我总是单击该行的文件名列部分)并且它太长了,无法像下面这样水平滚动:
The problem is that now I don't see the Keep
column, so I have to manually scroll back, to see if I marked the file or not. 问题在于,现在我看不到“ Keep
列,因此必须手动向后滚动,以查看是否标记了文件。 So to solve this, I saw many answers on SO suggesting editing the XAML part of the MainWindow
. 因此,为了解决这个问题,我在SO上看到了许多答案,建议编辑MainWindow
的XAML部分。 The problem is, this is my XAML file: 问题是,这是我的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>
The important part is, that I don't define columns myself and this is how I fill up the datagrid: 重要的是,我自己没有定义列,这就是我填充数据网格的方式:
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;
}
So I do it by changing the ItemSource
in code. 因此,我通过更改代码中的ItemSource
来实现。 And if I would define the columns myself and also change ItemSource
like this, I would have 4 columns instead of 2. So is there a way to prevent this autoscroll, when I am implementing it this way? 并且,如果我自己定义列并像这样更改ItemSource
,我将拥有4列而不是2列。那么当我以这种方式实现它时,有没有办法防止这种自动滚动?
Decided to make this another answer as this should be the preferred way to go since it gives you a lot more control over your layout: 决定做出另一个答案,因为这应该是首选方法,因为它可以使您更好地控制布局:
put this in your XAML: 将其放在您的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>
there should be no changes needed in your code behind. 后面的代码中无需进行任何更改。
Datagrid should have an attached property for this: https://docs.microsoft.com/de-de/dotnet/api/system.windows.controls.scrollviewer.horizontalscrollbarvisibility?view=netframework-4.8 Datagrid应该为此具有一个附加属性: https ://docs.microsoft.com/de-de/dotnet/api/system.windows.controls.scrollviewer.horizontalscrollbarvisibility?view=netframework-4.8
Set this to "Disabled": 将此设置为“已禁用”:
Scrollviewer.HorizontalScrollBarVisibility="Disabled"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.