Increase columns width in Silverlight DataGrid to fill whole DG width

前端 未结 6 761
生来不讨喜
生来不讨喜 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));
                }
            }
        }
    
    0 讨论(0)
  • 2021-02-05 12:30

    Building on Henrik P's answer, this solution simply straightens out the bug with Width='*' so that you can set any column to be proportional like you can on a grid:

        private void DgSQLDataSizeChanged(object sender, SizeChangedEventArgs e)
        {
            var myDataGrid = (DataGrid)sender;
    
            // Do not change column size if Visibility State Changed
            if (myDataGrid.RenderSize.Width == 0) return;
    
            double totalActualWidthOfNonStarColumns = myDataGrid.Columns.Sum(
                c => c.Width.IsStar ? 0 : c.ActualWidth);
    
            double totalDesiredWidthOfStarColumns = 
                myDataGrid.Columns.Sum(c => c.Width.IsStar ? c.Width.Value : 0);
    
            if ( totalDesiredWidthOfStarColumns == 0 ) 
                return;  // No star columns
    
            // Space available to fill ( -18 Standard vScrollbar)
            double spaceAvailable = (myDataGrid.RenderSize.Width - 18) - totalActualWidthOfNonStarColumns;
    
            double inIncrementsOf = spaceAvailable/totalDesiredWidthOfStarColumns;
    
            foreach (var column in myDataGrid.Columns)
            {
                if ( !column.Width.IsStar ) continue;
    
                var width = inIncrementsOf * column.Width.Value;
                column.Width = new DataGridLength(width, DataGridLengthUnitType.Star);
            }
        }
    

    I liked Henrik's answer but needed two columns to fill the extra space, like a grid.

    0 讨论(0)
  • 2021-02-05 12:32

    Tested only in WPF, not in Silverlight:

    I set up in WPF 3.5 SP1 and it works perfect, no guaranties about Silverlight, but if it works it's indeed charming.

    <data:DataGridTextColumn Header="Time" Binding="{Binding}" Width="*" />
    
    0 讨论(0)
  • 2021-02-05 12:40

    You can set it programmatically:

    var col = new DataGridTextColumn();
    col.Width = new DataGridLength(100, DataGridLengthUnitType.Star);                        
    Grid1.Columns.Add(col);
    
    0 讨论(0)
  • 2021-02-05 12:50

    In silverlight 4: simply set ColumnWidth="*" on the DataGrid

    <data:DataGrid ColumnWidth="*" />
    
    0 讨论(0)
  • 2021-02-05 12:53

    I have created an attached property for the DataGrid that lets you do this in XAML:

    <UserControl 
        xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  
        x:Class="GridProperties.Page"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:gp="clr-namespace:GridProperties">
    <Grid x:Name="LayoutRoot" Background="White">
        <data:DataGrid gp:GridEx.StarColumn="2">
            <data:DataGrid.Columns>
                <data:DataGridTextColumn Header="Column 1"/>
                <data:DataGridTextColumn Header="Column 2"/>
                <data:DataGridTextColumn Header="Column 3"/>
            </data:DataGrid.Columns>
        </data:DataGrid>
    </Grid>
    </UserControl>
    
    0 讨论(0)
提交回复
热议问题