WPF Grid Horizontal Scrollbar instead of column resizing

529 Views Asked by At

I have a Grid with many columns.

On small pc screens, the columns are automatically resized. Instead of this default behavior, I want an horizontal scrollbar (and the columns not to be resized).

If I set the MinWidth property on each columns, I have the scrollbar but I can't resize the columns anymore (not enough room). I also couldn't succeed with ScrollViewer (and it hurt the performances).

How can I get the scrollbar AND be able to resize the columns ?

<Grid>
    <DataGrid Height="200" Width="200" HorizontalScrollBarVisibility="Visible">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Column 1" Binding=.../>
            <DataGridTextColumn Header="Column 2" Binding=.../>
            <DataGridTextColumn Header="Column 3" Binding=.../>
            <DataGridTextColumn Header="Column 4" Binding=.../>
           [...]
        </DataGrid.Columns>
    </DataGrid>
</Grid>
1

There are 1 best solutions below

0
On

I tried your code and I get both the scroll bar for the grid when data doesn't fit, each column auto fits to the largest text size, and the columns are resizeable.

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            LoadData();
        }
       private void LoadData()
        {
            DataTable foo = new DataTable();
            for (int i = 0; i < 20; i++)
            {
                foo.Columns.Add(new DataColumn("column" + i, typeof(String)));
            }
            for (int i = 0; i < 20; i++)
            {
                DataRow dr = foo.NewRow();
                foreach (DataColumn dc in foo.Columns)
                {
                    if(i%5 ==0)
                    {
                        dr[dc] = string.Format("Looong and wide Data value {0} for cell.", i);
                    }
                    else
                    {
                        dr[dc] = string.Format("Data value {0} for cell.", i);
                    }
                }
                foo.Rows.Add(dr);
            }
            dataGrid.ItemsSource = foo.DefaultView;
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //cts.Cancel();
            dataGrid.EnableRowVirtualization = !dataGrid.EnableRowVirtualization;
            LoadData();
        }

    }
<Window x:Class="WpfApp1.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:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button Click="Button_Click" Content="Toggle Virtualization" HorizontalAlignment="Left" Margin="59,47,0,0" VerticalAlignment="Top" Width="75"/>
        <DataGrid HorizontalAlignment="Left" x:Name="dataGrid" AutoGenerateColumns="True"
             Width="695"     Height="257" Margin="59,93,0,0" 
                  VerticalAlignment="Top" > 
        </DataGrid> 
    </Grid>
</Window>