Set DataGrid size to fit all its contents
I need to make DataGrid size to fit all its contents (even if DataGrid will be bigger than it's parent). Is开发者_StackOverflow社区 it possible?
So, the answer I have found... just put DataGrid into StackPanel with vertical orientation. It has unbounded height, so the grid will take as much space as needed.
Edit: Re: Original Poster's Comment
Howdy again,
The real answer to your question is No--I'm sure you don't want to hear that. There is no way to get a child to break away and do its own thing.
However, there are patterns we can use to work around the problem. These work arounds give an effect similar to what you want. The hardest is breaking the limitations determined by the DataGrid's parent. From what I can tell, you only have two routes you can go:
Option 1: Refactor all of parents to resize with the DataGrid. The best way to do this is to use MinWidth and MinHeight properties on everything. By default they will use their size, but if one of their children needs more room, they will expand. Doing this allows the DataGrid to expand as its needs require.
<Grid MinWidth="500" MinHeight="500">
<sdk:DataGrid ItemsSource="{Binding Items}">
</Grid>
Option 2: Place your DataGrid over your "parent" controls. Think HTML/CSS. This is the trickiest concept, because you have to "know" where to put the DataGrid and how it will grow. You can add a clipping path to make the DataGrid look like it is a child of the Grid, but that is an answer to another post.
<Grid Width="500", Height="500"/>
<sdk:DataGrid ItemsSource="{Binding Items}" Margin="300,300,0,0"/>
Once again, I hope this helps. Let me know if you need anything else.
Howdy,
I'm assuming you want scrollbars on the DataGrid.
By default the DataGrid will generate all columns. But, because it doesn't enable the scrollbars by default, it tries to fit all of the columns it finds into one screen. You can enable these scrollbars by setting the DataGrid's ScrollViewer to show them.
If this isn't what you need, let me know.
Have a great day!
<sdk:DataGrid
ItemsSource="{Binding Items}"
AutoGenerateColumns="True"
ScrollViewer.HorizontalScrollBarVisibility="Visible"
ScrollViewer.VerticalScrollBarVisibility="Visible"/>
P.S. If you are setting up your columns manually, do not set Width="*"
. Setting Width="*"
tells the DataGrid to only use the remainder width available in one screen.
Avoid this! <sdk:DataGridTextColumn ... Width="*"/>
精彩评论