技术开发 频道

Silverlight DataGrid控件列宽自动填充

    【IT168 技术文档】在WinForm中,将DataGridView控件的列的AutoSizeMode属性设置为Fill,然后将FillWeight属性设置为列宽所占的权重,这样可实现列宽自动填充列,下图说明自动填充宽度的算法:

1
 

  但是在Silverlight中,DataGrid控件的列宽只有四种模式:Auto、Pixel、SizeToCells、SizeToHeader,没有Fill模式,无法实现自动填充列宽。

  那怎么实现此功能呢?用自定义模板?似乎比较麻烦,并且很难实现重用。看来只有用自定义控件了,先初步分析,首先,我们要控制列宽,肯定得处理DataGrid的SizeChanged事件;其次,我们必须定义DataGrid中每列宽度的填充模式;最后,还得定义每列宽度所占的权重。后两个步骤如何实现呢?Silverlight DataGrid控件中的列有三种类型DataGridTextColumn、DataGridCheckBoxColumn以及DataGridTemplateColumn,如果我们再分别为这三种类型创建子类来添加自定义属性显然太麻烦。

  DataGrid需要知道每个DataGridColumn中的宽度模式及填充比例,以便在DataGrid SizeChanged事件中设置每列的宽度,但是我们又要避免重新去定义每个DataGridColumn类型。Silverlight已经为此中问题提供了一种设计模式:即附加属性。我们常用的Grid布局控件,其中Grid.Row及Grid.Column属性即为附加属性,添加到Grid中的控件并没有Row和Column属性,但是通过Grid的Row和Column附加属性即可通知Grid控件应该被放到哪行哪列。同理,我们为DataGrid添加一个附加属性,即可让DataGrid中的每一列向DataGrid说明该列的填充模式和宽度所占的权重。为了简便,我们添加一个附加属性:WidthWeight,int类型,如果为0表示该列为固定宽度,否则表示该列所占权重。

  关于附加属性,请参考Silverlight SDK文档,有中文版本,简单易学:)

  贴代码:

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>
        
/// 右ò边?间?距à,?可é看′着?为aDataGrid的?滚?动ˉ条?宽í度è
        
/// </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,我们已经实现自己的DataGrid控件,下一步就是如何使用了:

  最简单的办法,在Silverlight项目上单击右键,选择生成,完成后,打开我们的xaml文件,此时工具箱中会出现我们的AutoFillDataGrid控件,拖动到xaml文件中即可。然后就像使用DataGrid控件一样,向AutoFillDataGrid中添加列,关键点是在需要自动填充列宽的DataGridColumn上设置AutoFillDataGrid.WidthWeight属性。

  示例源码下载 <---Visual 2010,如果使用Visual 2008请自己创建新工程,然后粘贴代码

0
相关文章