0
votes

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?

1
You say yourself that the DataGrid is getting resized because you set it to stretch. What do you want to happen?E Mett
I want the datagrid not to stretch, when the columns are added. But I want it to stretch with columns when user resizes the window itself. The window should be smaller than the whole width of screen, when it is opened.danrodi

1 Answers

0
votes

To fix this problem, the size of the window must be set on creation with the following lines of code:

window.Width = 916;
window.Height = 633;

Also the following line of code has to be removed, as it resizes the window after setting it's initial size:

window.SizeToContent = System.Windows.SizeToContent.WidthAndHeight;

Thanks to E Mett for leading me to the solution.