我有一个绑定到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附带宽度为“*”的列。
答案 0 :(得分: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));
}
}
}
结果:
答案 1 :(得分:8)
我在WPF 3.5 SP1中设置它完美无瑕,不保证Silverlight,但如果它有效,那确实很有魅力。
<data:DataGridTextColumn Header="Time" Binding="{Binding}" Width="*" />
答案 2 :(得分: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>
答案 3 :(得分: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的答案,但需要两列来填充额外的空间,就像网格一样。
答案 4 :(得分:1)
在silverlight 4中:只需在DataGrid上设置ColumnWidth =“*”
即可<data:DataGrid ColumnWidth="*" />
答案 5 :(得分:0)
您可以通过编程方式进行设置:
var col = new DataGridTextColumn();
col.Width = new DataGridLength(100, DataGridLengthUnitType.Star);
Grid1.Columns.Add(col);