Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide WPF DataGrid Scrollbar

Tags:

c#

wpf

datagrid

I am new to WPF. I was having bit different requirement with DataGrid so i transform it as below but i dont want any of the scrollbar but its not getting hidden. I have used LayoutTransform here row becomes column and column becomes row.

<DataGrid x:Name="fuelDataGrid" Width="200" Height="900" 
          AutoGenerateColumns="False"
          ItemsSource="{Binding Pump}"
          HorizontalAlignment="Left" FontSize="14"
          VerticalAlignment="Top" 
          HeadersVisibility="None"  
          CanUserReorderColumns="False" 
          CanUserResizeColumns="False" 
          CanUserResizeRows="False" 
          CanUserSortColumns="False" 
          Cursor="Hand" SelectionMode="Single" 
          HorizontalScrollBarVisibility="Disabled" 
          CanUserAddRows="False" 
          CanUserDeleteRows="False" 
          PreviewMouseLeftButtonUp="dataGrid2_PreviewMouseLeftButtonUp"  
          VerticalScrollBarVisibility="Disabled" 
          ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
          ScrollViewer.VerticalScrollBarVisibility="Disabled" 
          MouseDoubleClick="DataGrid_MouseDoubleClick" 
          TargetUpdated="DataGrid_OnTargetUpdated">

    <DataGrid.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" 
           Color="#FFCBCBCB"/>
    </DataGrid.Resources>

    <DataGrid.ColumnHeaderStyle>
        <Style TargetType="{x:Type DataGridColumnHeader}">
            <Setter Property="LayoutTransform">
                <Setter.Value>
                    <TransformGroup>
                        <RotateTransform Angle="90"/>
                    </TransformGroup>
                </Setter.Value>
            </Setter>
            <Setter Property="Width" Value="65"/>
            <!--<Setter Property="Height" Value="30"/>-->
        </Style>
    </DataGrid.ColumnHeaderStyle>

    <DataGrid.LayoutTransform>
        <TransformGroup>
            <RotateTransform Angle="-90"/>
        </TransformGroup>
    </DataGrid.LayoutTransform>

    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="LayoutTransform">
                <Setter.Value>
                    <TransformGroup>
                        <RotateTransform Angle="90"/>
                    </TransformGroup>
                </Setter.Value>
            </Setter>
            <Setter Property="Width" Value="100"/>
            <Setter Property="Foreground" Value="Black" />
            <!--<Setter Property="Height" Value="30"/>-->
        </Style>
    </DataGrid.CellStyle>

    <DataGrid.Columns>
        <!--    <DataGridTextColumn Header="Old Price" Binding="{Binding Row1Value}" /> -->
        <DataGridTemplateColumn Header="Old Price">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                 // Code here
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Current Price">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                // Code here
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn> 
</DataGrid.Columns>

I tried below with Hidden as well. But nothing is working.

VerticalScrollBarVisibility="Hidden" 
ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
ScrollViewer.VerticalScrollBarVisibility="Hidden"
HorizontalScrollBarVisibility="Hidden"
like image 541
Anchit Pancholi Avatar asked Sep 18 '25 09:09

Anchit Pancholi


1 Answers

This simple test case gives me a data grid with no scrollbars:

<DataGrid
    HorizontalScrollBarVisibility="Hidden"
    VerticalScrollBarVisibility="Hidden"
    ItemsSource="{Binding Items}" />

It is bound to a collection of 50 strings. The first few are visible and the rest go out of bounds of the window. No scrollbars appear. Maybe try simplifying your test case until you find the issue?

EDIT: I tried copying your entire data grid xaml and still could not reproduce the issue. Of course, I had to remove the parts that call into unknown code-behind methods, and I substituted a TextBlock in the data templates where you typed "// Code here". So, either there is something different about our environments, or the code that is causing the issue is not included in your question.

Please include the simplest test case that reproduces the issue you are seeing so that someone can actually help you solve it.

like image 120
Xavier Avatar answered Sep 20 '25 22:09

Xavier