C# 增加 Silverlight DataGrid 中的列宽以填充整个 DG 宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/894991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Increase columns width in Silverlight DataGrid to fill whole DG width
提问by Henrik P. Hessel
I have a DataGrid Control which is bound to a SQL Table.
我有一个绑定到 SQL 表的 DataGrid 控件。
The XAML Code is:
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>
Is it possible increase the single columns sizes to fill out the complete width of the datagrid?
是否可以增加单列大小来填充数据网格的完整宽度?
Thanks,
Henrik
谢谢,
亨里克
Edit: Columns with "*" as width are coming with the Silverlight SDK 4.
编辑:Silverlight SDK 4 附带带有“*”作为宽度的列。
采纳答案by Henrik P. Hessel
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));
}
}
}
回答by Shimmy Weitzhandler
Tested only in WPF, not in Silverlight:
仅在 WPF 中测试,不在 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.
我在 WPF 3.5 SP1 中进行设置,它运行完美,Silverlight 没有任何保证,但如果它运行正常,它确实很迷人。
<data:DataGridTextColumn Header="Time" Binding="{Binding}" Width="*" />
回答by Michael S. Scherotter
I have created an attached propertyfor the DataGrid that lets you do this in XAML:
我为 DataGrid创建了一个附加属性,允许您在 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>
回答by HiredMind
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:
以 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);
}
}
I liked Henrik's answer but needed two columns to fill the extra space, like a grid.
我喜欢 Henrik 的回答,但需要两列来填充额外的空间,就像一个网格。
回答by felickz
In silverlight 4: simply set ColumnWidth="*" on the DataGrid
在 Silverlight 4 中:只需在 DataGrid 上设置 ColumnWidth="*"
<data:DataGrid ColumnWidth="*" />
回答by devi
You can set it programmatically:
您可以以编程方式设置它:
var col = new DataGridTextColumn();
col.Width = new DataGridLength(100, DataGridLengthUnitType.Star);
Grid1.Columns.Add(col);