1
votes

I've got unusual problem that I can't solve. I want to use DataGrid with fixed column sizes that will size up to parent. Structure looks more less like this:

UserControl put on ScrollViewer sizes correctly to parent unless it reaches MinWidth then scrollbar appears. All controls on UserControl are stretched when someone resize window. Only individual is DataGrid. It doesn't want to enable horizontal scroll no matter how much I try it always makes UserControl scale to it's width. This of course enables scroll under UserControl which I want to prevent.

in short I want DataGrid to take up 100% of available width but not more.

    <Window>
        <ScrollViewer>
            <UserControl>
                <Grid>
                    <DataGrid>

                    </DataGrid>
                </Grid>
            </UserControl>
        </ScrollViewer>
    </Window>

I also tried to do something like that, but it failed :(

    <Window>
        <ScrollViewer>
            <UserControl>
                <Grid>
                    <ScrollViewer>
                        <DockPanel>
                            <DataGrid>

                            </DataGrid>
                        </DockPanel>
                    </ScrollViewer>
                </Grid>
            </UserControl>
        </ScrollViewer>
    </Window>

Also hooking up to SizeChanged and initially setting Width of DataGrid to some small fixed value event did not make the test. Probably because I'm operating on UserControl derived code.

I need suggestions.

1

1 Answers

2
votes

Get rid of the ScrollViewer around the DataGrid entirely, that will cause the DataGrid to not know the width of its parent control - it tells the DataGrid that it has infinite space.

Just try placing the DataGrid as in your first example, and setting HorizontalAlignment=Stretch on the DataGrid. That should be the default on the Grid containing the DataGrid, but set it there too just to be sure. Also make sure you don't have the width property set (MinWidth should be fine).

Edit your code into your question once you've given it a shot.