【发布时间】:2019-06-22 16:33:51
【问题描述】:
基本上,我有一个应用程序可以帮助我对我的照片和电影收藏进行分类/分类。这真的很简单,我有一个DataGrid,其中列出了特定文件夹中的所有文件。有两列:Keep 和 Filename。我将使用箭头向下此 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 列。那么当我以这种方式实现时,有没有办法防止这种自动滚动?
【问题讨论】: