豪仕知识网--知识就是力量!

微信
手机版
生活常识

基于异常类型的异常处理策略解决EnterLib异常处理框架最大的局限-用于异常处理的关键字

作者 :钱途无量 2024-01-03 14:27:56 围观 : 评论

基于异常类型的异常处理策略解决EnterLib异常处理框架最大的局限-用于异常处理的关键字

豪士君测试所用平台

基于异常类型的异常处理策略解决EnterLib异常处理框架最大的局限,一起来看看吧,希望能帮助到您,更多请关注豪仕知识网。

◐◐◐◐●☛█▼▲豪仕知识网███████http://www.haOZ.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

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

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

◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲豪仕知识网HtTp://▲▼▲▼▲

那怎么实现此功能呢?用自定义模板?似乎比较麻烦,并且很难实现重用。看来只有用自定义控件了,先初步分析,首先,我们要控制列宽,肯定得处理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文档,有中文版本,简单易学:)

◐◐◐◐●☛█▼▲豪仕知识网███████豪仕知识http://www.Haoz.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

代码:

HTTP://WWW.haoz.net豪仕知识网采集不好玩哦◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐◐撒旦法师打发斯蒂芬

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 queue = new Queue();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.MaxWidth){actualWidth = column.MaxWidth;}column.Width = new DataGridLength(actualWidth);}}///

/// 右ò边?间?距à,?可é看′着?为aDataGrid的?滚?动ˉ条?宽í度è/// http://www.haoz.net●☛█▼▲◐●☛█▼▲◐◐◐◐●☛█▼▲◐豪仕知识网●☛█▼▲豪仕知识网

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属性。

◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲HtTp://wWW.haoz.net豪仕知识网●●●●●●●●●●●●●●●●●●●●●●●●●●

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

豪仕知识网http://www.haoz.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

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

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

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

◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲◐◐◐◐●☛█▼▲HTTP://WWW.hAoz.net███████████████████████████东方金报网

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文档,有中文版本,简单易学:)

代码:

◐◐◐◐●☛█▼▲豪仕知识网███████http://www.haOZ.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

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 queue = new Queue();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.MaxWidth){actualWidth = column.MaxWidth;}column.Width = new DataGridLength(actualWidth);}}///

/// 右ò边?间?距à,?可é看′着?为aDataGrid的?滚?动ˉ条?宽í度è///

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控件,下一步就是如何使用了:

http://www.haoz.net●☛█▼▲◐●☛█▼▲◐◐◐◐●☛█▼▲◐豪仕知识网●☛█▼▲豪仕知识网

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

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

基于异常类型的异常处理策略解决EnterLib异常处理框架最大的局限文章到此结束,字数约6925字,希望可以帮助到大家。豪仕知识网往后会继续推荐基于异常类型的异常处理策略解决EnterLib异常处理框架最大的局限相关内容。

◐◐◐◐●☛█▼▲豪仕知识网███████http://www.haOZ.net▼▲▼▲▼▲▼▲▼●●●●●●●▼▲▼▲▼▲

相关文章