In winform, set the autosizemode attribute of the column in The datagridview control to fill, and then set the fillweight attribute to the weight occupied by the column width, so that the column width can be automatically filled, describes the algorithm for automatically filling the width:
However, in Silverlight, the column width of the DataGrid Control is only in four modes: auto, pixel, sizetocells, and sizetoheader. The column width cannot be automatically filled without the fill mode.
How can this function be implemented? Use a custom template? It seems complicated and difficult to implement reuse. It seems that only custom controls are used. First, we need to make a preliminary analysis. First, we need to control the column width and certainly have to handle the sizechanged event of the DataGrid. Second, we must define the fill mode of each column width in the DataGrid. Finally, we must define the weight of each column width. How can we implement the next two steps? The columns in the Silverlight DataGrid Control have three types: datagridtextcolumn, datagridcheckboxcolumn, and datagridtemplatecolumn. If we create subclasses for these three types to add custom attributes, it is obviously too troublesome.
The DataGrid needs to know the width mode and fill ratio of each datagridcolumn to set the width of each column in The DataGrid sizechanged event, but we need to avoid redefining each datagridcolumn type. Silverlight provides a design pattern for this problem: Additional attributes. We commonly use grid layout controls, where grid. row and grid. the column attribute is an additional attribute. The control added to the grid does not have the row and column attributes, but the row and column attributes of the grid can be used to notify the row and column of the grid control. Similarly, we add an additional attribute for the DataGrid to show each column in The DataGrid the fill mode and weight of the column width to the DataGrid. For ease of use, we add an additional Property: widthweight, int type. If it is 0, it indicates that the column is fixed width; otherwise, it indicates that the column occupies the weight.
For more information about additional attributes, see the Silverlight SDK documentation. The Chinese version is easy to learn :)
Code:
Hide row number copy code? Autofilldatagrid. CS
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
namespace Test
{
public class AutoFillDataGrid : DataGrid
{
public static readonly DependencyProperty WidthWeightProperty = DependencyProperty.RegisterAttached(
"WidthWeight", typeof(int), typeof(AutoFillDataGrid), new PropertyMetadata(0));
private double paddingRight = 0;
public AutoFillDataGrid()
{
this.SizeChanged += new SizeChangedEventHandler(AutoFillDataGrid_SizeChanged);
}
void AutoFillDataGrid_SizeChanged(object sender, SizeChangedEventArgs e)
{
int weight = 0;
double fix = 0;
int temp = 0;
Queue<DataGridColumn> queue = new Queue<DataGridColumn>();
foreach (DataGridColumn column in this.Columns)
{
temp = (int)column.GetValue(WidthWeightProperty);
if (temp > 0)
{
weight += temp;
queue.Enqueue(column);
}
else
{
fix += column.ActualWidth;
}
}
double width = e.NewSize.Width - this.paddingRight - fix - this.Padding.Left - this.Padding.Right;
double actualWidth = 0;
while (queue.Count > 0)
{
DataGridColumn column = queue.Dequeue();
temp = (int)column.GetValue(WidthWeightProperty);
actualWidth = width * (double)temp / weight;
if (actualWidth < column.MinWidth)
{
actualWidth = column.MinWidth;
}
if (actualWidth > column.MaxWidth)
{
actualWidth = column.MaxWidth;
}
column.Width = new DataGridLength(actualWidth);
}
}
/// <summary>
/// Right side? Between? ,? Keré? Which is adatagrid? Roll? Dynamic record? Kuan ídoué
/// </summary>
public double PaddingRight
{
get
{
return this.paddingRight;
}
set
{
this.paddingRight = value;
}
}
public static void SetWidthWeight(DataGridColumn column, int value)
{
column.SetValue(WidthWeightProperty, value);
}
public static int GetWidthWeight(DataGridColumn column)
{
return (int)column.GetValue(WidthWeightProperty);
}
}
}
OK, we have implemented our own DataGrid Control. The next step is how to use it:
In the simplest way, right-click the Silverlight project, select generate, and open our XAML file. Our autofilldatagrid control will appear in the toolbox, drag it to The XAML file. Then, just like using the DataGrid Control, add columns to the autofilldatagrid. The key point is to set the autofilldatagrid. widthweight attribute on the datagridcolumn where the column width needs to be automatically filled.
Download the sample source code <--- visual 2010. If you use visual 2008, create a new project and paste the code.