Reputation: 1947
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. There are two columns: Keep
and 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). 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:
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. So to solve this, I saw many answers on SO suggesting editing the XAML part of the MainWindow
. The problem is, this is my XAML file:
<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. 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?
Upvotes: 1
Views: 1465
Reputation: 127
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:
<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.
Upvotes: 1
Reputation: 127
Datagrid should have an attached property for this: https://learn.microsoft.com/de-de/dotnet/api/system.windows.controls.scrollviewer.horizontalscrollbarvisibility?view=netframework-4.8
Set this to "Disabled":
Scrollviewer.HorizontalScrollBarVisibility="Disabled"
Upvotes: 0