How to implement automatic column width filling of the Silverlight DataGrid Control

Source: Internet
Author: User

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
  1. using System;
  2. using System.Net;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Documents;
  6. using System.Windows.Ink;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Animation;
  10. using System.Windows.Shapes;
  11. using System.Collections.Generic;
  12. namespace Test
  13. {
  14.     public class AutoFillDataGrid : DataGrid
  15.     {
  16.         public static readonly DependencyProperty WidthWeightProperty = DependencyProperty.RegisterAttached(
  17.             "WidthWeight", typeof(int), typeof(AutoFillDataGrid), new PropertyMetadata(0));
  18.         private double paddingRight = 0;
  19.         public AutoFillDataGrid()
  20.         {
  21.             this.SizeChanged += new SizeChangedEventHandler(AutoFillDataGrid_SizeChanged);
  22.         }
  23.         void AutoFillDataGrid_SizeChanged(object sender, SizeChangedEventArgs e)
  24.         {
  25.             int weight = 0;
  26.             double fix = 0;
  27.             int temp = 0;
  28.             Queue<DataGridColumn> queue = new Queue<DataGridColumn>();
  29.             foreach (DataGridColumn column in this.Columns)
  30.             {
  31.                 temp = (int)column.GetValue(WidthWeightProperty);
  32.                 if (temp > 0)
  33.                 {
  34.                     weight += temp;
  35.                     queue.Enqueue(column);
  36.                 }
  37.                 else
  38.                 {
  39.                     fix += column.ActualWidth;
  40.                 }
  41.             }
  42.             double width = e.NewSize.Width - this.paddingRight - fix - this.Padding.Left - this.Padding.Right;
  43.             double actualWidth = 0;
  44.            
  45.             while (queue.Count > 0)
  46.             {
  47.                 DataGridColumn column = queue.Dequeue();
  48.                 temp = (int)column.GetValue(WidthWeightProperty);
  49.                 actualWidth = width * (double)temp / weight;
  50.                 if (actualWidth < column.MinWidth)
  51.                 {
  52.                     actualWidth = column.MinWidth;
  53.                 }
  54.                 if (actualWidth > column.MaxWidth)
  55.                 {
  56.                     actualWidth = column.MaxWidth;
  57.                 }
  58.                 column.Width = new DataGridLength(actualWidth);
  59.             }
  60.         }
  61.         /// <summary>
  62. /// Right side? Between? ,? Keré? Which is adatagrid? Roll? Dynamic record? Kuan ídoué
  63.         /// </summary>
  64.         public double PaddingRight
  65.         {
  66.             get
  67.             {
  68.                 return this.paddingRight;
  69.             }
  70.             set
  71.             {
  72.                 this.paddingRight = value;
  73.             }
  74.         }
  75.         public static void SetWidthWeight(DataGridColumn column, int value)
  76.         {
  77.             column.SetValue(WidthWeightProperty, value);
  78.         }
  79.         public static int GetWidthWeight(DataGridColumn column)
  80.         {
  81.             return (int)column.GetValue(WidthWeightProperty);
  82.         }
  83.     }
  84. }


 


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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.