在Silverlight DataGrid中增加列的宽度以填充整个DG的宽度

问题描述 投票:16回答:6

我有一个绑定到SQL表的DataGrid控件。

XAML代码是:

<data:DataGrid x:Name="dg_sql_data" 
                       Grid.Row="1" 
                       Visibility="Collapsed" 
                       Height="auto" 
                       Margin="0,5,5,5"
                       AutoGenerateColumns="false"
                       AlternatingRowBackground="Aqua"
                       Opacity="80"
                       >
    <data:DataGrid.Columns>
        <data:DataGridTextColumn Header="Latitude" Binding="{Binding lat}" />
        <data:DataGridTextColumn Header="Longitude" Binding="{Binding long}" />
        <data:DataGridTextColumn Header="Time" Binding="{Binding time}" />
    </data:DataGrid.Columns>
</data:DataGrid>

是否有可能增加单列大小以填充数据网格的整个宽度?

谢谢,亨里克

编辑:Silverlight SDK 4附带宽度为“ *”的列。

c# silverlight datagrid width
6个回答
11
投票

解决方案:

    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));
            }
        }
    }

8
投票

仅在WPF中测试,在Silverlight中未测试:

我在WPF 3.5 SP1中进行了设置,它可以完美运行,没有关于Silverlight的保证,但如果可以,那么确实很吸引人。

<data:DataGridTextColumn Header="Time" Binding="{Binding}" Width="*" />

1
投票

我为DataGrid创建了attached property,可让您在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>

1
投票

基于Henrik P的答案,此解决方案仅用Width='*'消除了错误,因此您可以像设置网格一样将任何列设置为成比例的:

    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);
        }
    }

我喜欢Henrik的答案,但需要两列来填充额外的空间,例如网格。


1
投票

在Silverlight 4中:只需在DataGrid上设置ColumnWidth =“ *”

<data:DataGrid ColumnWidth="*" />

0
投票

您可以通过编程方式进行设置:

var col = new DataGridTextColumn();
col.Width = new DataGridLength(100, DataGridLengthUnitType.Star);                        
Grid1.Columns.Add(col);
© www.soinside.com 2019 - 2024. All rights reserved.