wpflearner
wpflearner

Reputation: 121

How to disable scrolling inside datagrid in wpf c#

I have done VerticalScrollBarVisibility="Disabled" because i dont want the content inside datagrid to be viewed which crosses the assigned height. I am not able to see scroll bar after giving the above statement.but i still can scroll down and see the rows.Can someone tell me how do i disable scrolling all together? Thanks

Upvotes: 12

Views: 18896

Answers (5)

LorenzoB
LorenzoB

Reputation: 51

The accepted solution did not work for me since I needed row selection. I solved all my issues by disabling datagrid panning (for mouse dragging) and handling key down events (for keyboard). In datagrid XAML:

ScrollViewer.PanningMode="None"

And:

PreviewKeyDown="OnDatagridPreviewKeyDown"

In code behind:

private void OnDatagridPreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Right || e.Key == Key.Left)
        e.Handled = true;
}

HTH.

Edit: My answer applies to columns but everything is similar for rows.

Upvotes: 2

Mohamed Mansour
Mohamed Mansour

Reputation: 40199

The correct approach would be to disable the Hit target

DataGrid.IsHitTestVisible = false;

Upvotes: 7

ouflak
ouflak

Reputation: 2514

Just wanted to add an answer that applied to columns. On occasion, you may find that the Datagrid will scroll to extra columns that you would prefer remain invisible, especially if the height is specifically adjusted so that any additional columns are invisible. I simply attached a Loaded handler to the Datagrid and set all of the additional columns to a width of 0 and made them hidden.

Upvotes: 0

Ilya Serbis
Ilya Serbis

Reputation: 22333

Allow DataGrid to show all it's content (so it needn't scroll bar):

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <DataGrid x:Name="DataGrid" />
</Grid>

You can than put the result in any place. It'll cut down it's size to the owner's dimensions

Upvotes: 3

ms87
ms87

Reputation: 17492

Once you've disabled the VerticalScrollBarVisibility for your DatGrid, you need to disable the ScrollViewer's scroll functionality like this:

ScrollViewer.CanContentScroll="False"

But when you do his make sure that you have already defined a standard height for your entire DataGrid and your DataGrid rows such that the user can see all the rows you want them to see, otherwise the chopped off rows will not be displayed and the user can not scroll down to see them.

Hope this helps.

Upvotes: 7

Related Questions