Increase columns width in Silverlight DataGrid to fill whole DG width

前端 未结 6 760
生来不讨喜
生来不讨喜 2021-02-05 11:49

I have a DataGrid Control which is bound to a SQL Table.

The XAML Code is:



        
6条回答
  •  隐瞒了意图╮
    2021-02-05 12:28

    Solution:

        void dg_sql_data_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            DataGrid myDataGrid = (DataGrid)sender;
            // Do not change column size if Visibility State Changed
            if (myDataGrid.RenderSize.Width != 0)
            {
                double all_columns_sizes = 0.0;
                foreach (DataGridColumn dg_c in myDataGrid.Columns)
                {
                    all_columns_sizes += dg_c.ActualWidth;
                }
                // Space available to fill ( -18 Standard vScrollbar)
                double space_available = (myDataGrid.RenderSize.Width - 18) - all_columns_sizes;
                foreach (DataGridColumn dg_c in myDataGrid.Columns)
                {
                    dg_c.Width = new DataGridLength(dg_c.ActualWidth + (space_available / myDataGrid.Columns.Count));
                }
            }
        }
    

提交回复
热议问题