I have a datagrid with multiple columns inside my WPF form. When the form is opened and the columns are added to the datagrid, the form gets automatically resized to the width of the screen.
Now I know that this happens because I have set the datagrid alignments to stretch and the column widths to * in my code. But these need to be set, as I want the datagrid to change it's size and the columns to fill the datagrid width when the user resizes the window (not when the window is opened).
The form itself is here:
<UserControl
/*...*/
d:DesignHeight="590" d:DesignWidth="896">
<Grid x:Name="Form" Loaded="Form_Loaded" MinWidth="896" MinHeight="590">
<Label x:Name="label" Content="Label" HorizontalAlignment="Left" Margin="10,8,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.479,0.577" Height="27"/>
<ComboBox x:Name="combo" HorizontalAlignment="Left" Margin="68,10,0,0" VerticalAlignment="Top" Width="200" SelectionChanged="combo_SelectionChanged" Height="23"/>
<Button x:Name="buttonOne" Content="Button" HorizontalAlignment="Right" Margin="0,10,10,0" VerticalAlignment="Top" Width="75" Height="23"/>
<TextBox x:Name="box" HorizontalAlignment="Right" Height="23" Margin="0,10,95,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="200"/>
<DataGrid x:Name="view" HorizontalAlignment="Stretch" Margin="10,43" VerticalAlignment="Stretch" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" CanUserReorderColumns="False" SelectionMode="Single" IsReadOnly="True" ItemsSource="{Binding Path=List}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Width="*" Binding="{Binding Path=Name}" Visibility="Visible"/>
/*...*/
</DataGrid.Columns>
</DataGrid>
<Button x:Name="buttonTwo" Content="Button" HorizontalAlignment="Right" Margin="0,0,10,10" DockPanel.Dock="Right" Width="75" Height="23" Click="buttonTwo_Click" VerticalAlignment="Bottom"/>
</Grid>
</UserControl>
And the creation of it is here:
System.Windows.Window window = new System.Windows.Window();
Form form = new Form();
window.Content = form;
window.ResizeMode = System.Windows.ResizeMode.CanResizeWithGrip;
window.MinWidth = 916;
window.MinHeight = 633;
window.Title = "Form";
window.SizeToContent = System.Windows.SizeToContent.WidthAndHeight;
Uri iconUri = new Uri(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\Image\\image.ico", UriKind.RelativeOrAbsolute);
window.Icon = BitmapFrame.Create(iconUri);
window.ShowDialog();
I tried putting the datagrid inside a separate panel that has fixed width and height. I have also changed the window size after adding the columns, but none of these solutions work as planned.
How can I avoid the resizing of datagrid and the window (so that it is later resizable), when the window has been opened and the columns are added?