DataGrid的Header的自定义功能

上传人:汽*** 文档编号:486802916 上传时间:2024-02-02 格式:DOCX 页数:11 大小:78.31KB
返回 下载 相关 举报
DataGrid的Header的自定义功能_第1页
第1页 / 共11页
DataGrid的Header的自定义功能_第2页
第2页 / 共11页
DataGrid的Header的自定义功能_第3页
第3页 / 共11页
DataGrid的Header的自定义功能_第4页
第4页 / 共11页
DataGrid的Header的自定义功能_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《DataGrid的Header的自定义功能》由会员分享,可在线阅读,更多相关《DataGrid的Header的自定义功能(11页珍藏版)》请在金锄头文库上搜索。

1、DataGrid 的 Header 的自定义功能下面我们来学习 DataGrid 的 Header 的自定义功能。我们实现对Grid的排序,但是用的是Grid自动产生的Header,现在我们用自定义的控 件来代替系统自动生成的Header,并且在Header下面添加一个可以对数据进行过滤的功能。因为过去没有用过silverlight,在上听说在2.0RC出来之前,自定义 ColumnHeader很容易,只需要设置Column.Header为某个控件就可以了,但是现在全变了, Column.Header变成了表头的内容了,要更改 Header,必须用做一个新的Style,赋给 Column.He

2、aderStyle。不是太明白微软问什么要这样做,把一个简单的问题搞的很复杂,对 一个新手来说,做一个Style真的很费事。按照字面的意思,我认为Style应该是用来控制控 件的展现方式,有点像WEB上的CSS,但是这里的Style不但可以控制如何展示控件,还 要设置用什么控件,如何绑定数据,甚至还是事件的处理等等,感觉像个像个自定义控件, 但是又没有自定义控件好用。好了,废话不说了,先看效果:EmpT-cyes则11II1Chai19.DOOO 101110 baxa 址 2U219.D00C 1740却8 12bottlesSyraple.oooc 13(2 eI bo战应Chef Ant

3、ons Cajun SenwningSIChef Antons Gumbp Mi斗Grandmas BavMntrfrry SpreadUncle Bdbs Orflanie Dn*d Psars25.D03QtD0._ 1 UJLlJLlJLlJ-rnrnrnHortiiwoods Cranbefry Sauce4D.D0Mishi Kobe NikuM300CIkura31-DQOC具体实现的过程:第一步,我们要做一个新的ColumnHeader。这里直接用来上 一位高手的代码,并且做了扩展,主要是添加了 Header的点击事件和过滤框的 事件,以及一个新的属性用来保存字段名。应为在实际的

4、项目中,我们不可能在 Grid上直接显示数据库的字段名,而是要用中文来显示。代码不是太复杂,就1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.不做解释了。GridHeader.cs1 TemplatePart(Name = GridHeader.HeaderTextElement, Type = typeof(FrameworkElement)2 TemplatePart(Name = GridHeader.FilterTextElement, Typ

5、e = typeof(TextBox)3 public class GridHeader : Control4 5 public delegate void HeaderClickEvent(string fieldname);6 public event HeaderClickEvent OnSort;78 public delegate void FilterTextEvent(string fieldname, string filtertext);9 public event FilterTextEvent OnFilter;1011 protected const string He

6、aderTextElement = btnHeaderText;12 protected const string FilterTextElement = txtFilerText;1314 Button btn_header;15 TextBox filterText;1617 #region Constructor18 public GridHeader()19 20 this.DefaultStyleKey = typeof(GridHeader);21 22 #endregion2324 #region HeaderText25 / 26 / Identifies the Header

7、Text dependency property.27 / 28 public static readonly DependencyProperty HeaderTextProperty = DependencyProperty.Register(HeaderText, typeof(string), typeof(GridHeader), null);2930 / 31 / Gets or sets the HeaderText possible Value of the int object.32 / 33 public string HeaderText34 35 get return

8、(string)GetValue(HeaderTextProperty); 36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.73.36 set SetValue(HeaderTextProperty, value); 37 38 #endregion HeaderText3940#region FilterText4142 / 43 / Identifies theFilterTextdependencyproperty.

9、44 / 45 public static readonlyDependencyPropertyFilterTextProperty = DependencyProperty.Register(FilterText, typeof(string), typeof(GridHeader), null);4647 / 48 / Gets or sets the FilterText possible Value of the string object.49 / 50 public string FilterText51 52 get return (string)GetValue(FilterT

10、extProperty); 53 set SetValue(FilterTextProperty, value); 54 55 #endregion FilterText5657 #region FieldText58 public static DependencyProperty FieldTextProperty = DependencyProperty.Register(FieldText, typeof(string), typeof(GridHeader), null);59 public string FieldText60 61 get return (string)GetVa

11、lue(FieldTextProperty); 62 set SetValue(FieldTextProperty, value); 63 6465 #endregion6667 public override void OnApplyTemplate()68 69 base.OnApplyTemplate();70 StackPanel sp = this.GetTemplateChild(LayoutRoot) as StackPanel;7172 btn_header = sp.Children0 as Button;73 filterText = sp.Children1 as Tex

12、tBox;74.7475.75 if (this.filterText != null)76.76 this.filterText.LostFocus += newRoutedEventHandler(filterText_LostFocus);77.77 if (this.btn_header != null)78.78 this.btn_header.Click += newRoutedEventHandler(header_Click);79.79 80.8081.81 void header_Click(object sender, RoutedEventArgs e)82.82 83.83 OnSort(FieldText);84.84 85.8586.86 void filterText_LostFocus(object sender, RoutedEventArgs e)87.87 88.88 OnFilter(FieldText, filterText.Text.Trim();89.89 90.90第二步,在Themes下添加一个Generic.xaml,内容如下:Generic1. 5. 6. 7. 8. 9. 10. Button x:Name=btnHeaderText Margin=0 BorderThickness=0 Content=Te

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 建筑资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号