WPF Datagrid column auto width broken

250 Views Asked by At

I have an DataGrid with ColumnWidth="*" and then application will started i see this: 1st image

I maximize window and all good:

2nd image

But. When i minimize my window, i see minimize columns:

3rd image

My maximize and minimize function:

private void MaxButton_MouseUp(object sender, MouseButtonEventArgs e)
    {
        if (e != null)
        {
            e.Handled = true;

            if (CustomMaximized)
            {
                TopResizeRectangles.Visibility = Visibility.Visible;
                DownResizeRectangles.Visibility = Visibility.Visible;

                CustomMaximized = false;
                Width = NormalWidth;
                Height = NormalHeight;
                //Left = NormalLeft;
                //Top = NormalTop;

                MaxButton.Style = Resources["Maximize"] as Style;
            }
            else
            {
                NormalWidth = Width;
                NormalHeight = Height;
                NormalLeft = Left;
                NormalTop = Top;

                TopResizeRectangles.Visibility = Visibility.Hidden;
                DownResizeRectangles.Visibility = Visibility.Hidden;

                CustomMaximized = true;
                Width = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
                Height = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
                Left = 0;
                Top = 0;

                MaxButton.Style = Resources["Minimize"] as Style;
            }
        }
    }

And when window size change i make this:

private void Page_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        foreach (var column in VisualisationScoreboardGrid.Columns)
            column.Width = 0;

        VisualisationScoreboardGrid.UpdateLayout();

        foreach (var column in VisualisationScoreboardGrid.Columns)
            column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);

        VisualisationScoreboardGrid.UpdateLayout();
    }

I use a custom solution because using just xaml ColumnWidth="*" under certain conditions gives a similar result to the above. How a make normal auto width for my columns?

0

There are 0 best solutions below