DataGrid的Header的自定義功能_第1頁(yè)
DataGrid的Header的自定義功能_第2頁(yè)
DataGrid的Header的自定義功能_第3頁(yè)
DataGrid的Header的自定義功能_第4頁(yè)
DataGrid的Header的自定義功能_第5頁(yè)
已閱讀5頁(yè),還剩5頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、DataGrid 的 Header 的自定義功能下面我們來(lái)學(xué)習(xí) DataGrid 的 Header 的自定義功能。我們實(shí)現(xiàn)對(duì)Grid的排序,但是用的是Grid自動(dòng)產(chǎn)生的Header,現(xiàn)在我們用自定義的控 件來(lái)代替系統(tǒng)自動(dòng)生成的Header,并且在Header下面添加一個(gè)可以對(duì)數(shù)據(jù)進(jìn)行過(guò)濾的功能。因?yàn)檫^(guò)去沒(méi)有用過(guò)silverlight,在上聽說(shuō)在2.0RC出來(lái)之前,自定義 ColumnHeader很容易,只需要設(shè)置Column.Header為某個(gè)控件就可以了,但是現(xiàn)在全變了, Column.Header變成了表頭的內(nèi)容了,要更改 Header,必須用做一個(gè)新的Style,賦給 Column.He

2、aderStyle。不是太明白微軟問(wèn)什么要這樣做,把一個(gè)簡(jiǎn)單的問(wèn)題搞的很復(fù)雜,對(duì) 一個(gè)新手來(lái)說(shuō),做一個(gè)Style真的很費(fèi)事。按照字面的意思,我認(rèn)為Style應(yīng)該是用來(lái)控制控 件的展現(xiàn)方式,有點(diǎn)像WEB上的CSS,但是這里的Style不但可以控制如何展示控件,還 要設(shè)置用什么控件,如何綁定數(shù)據(jù),甚至還是事件的處理等等,感覺(jué)像個(gè)像個(gè)自定義控件, 但是又沒(méi)有自定義控件好用。好了,廢話不說(shuō)了,先看效果:EmpT-cyes則11II1Chai19.DOOO 101110 baxa 址 2U219.D00C 1740EmpT-cyes則11II1Chai19.DOOO 101110 baxa 址 2U21

3、9.D00C 1740卻8 12bottlesSyraple.oooc 13(2 eI bo戰(zhàn)應(yīng)Chef Antons Cajun SenwningSIChef Antons Gumbp Mi斗Grandmas BavMntrfrry SpreadUncle Bdbs Orflanie Dn*d Psars25.D03QtD0._ 1 UJLlJLlJLlJ-rnrnrnHortiiwoods Cranbefry Sauce4D.D0Mishi Kobe NikuM300CIkura31-DQOC具體實(shí)現(xiàn)的過(guò)程:第一步,我們要做一個(gè)新的ColumnHeader。這里直接用來(lái)上 一位高手的代碼,

4、并且做了擴(kuò)展,主要是添加了 Header的點(diǎn)擊事件和過(guò)濾框的 事件,以及一個(gè)新的屬性用來(lái)保存字段名。應(yīng)為在實(shí)際的項(xiàng)目中,我們不可能在 Grid上直接顯示數(shù)據(jù)庫(kù)的字段名,而是要用中文來(lái)顯示。代碼不是太復(fù)雜,就.3.34.35.不做解釋了。GridHeader.csTemplatePart(Name = GridHeader.HeaderTextElement, Type = typeof(FrameworkElement)TemplatePart(Name = GridHeader.FilterTextElement, Type = typeof(TextBox)public class Gri

5、dHeader : Controlpublic delegate void HeaderClickEvent(string fieldname);public event HeaderClickEvent OnSort;78 public delegate void FilterTextEvent(string fieldname, string filtertext);9 public event FilterTextEvent OnFilter;1011 protected const string HeaderTextElement = btnHeaderText;12 protecte

6、d const string FilterTextElement = txtFilerText;1314 Button btn_header;15 TextBox filterText;16#region Constructorpublic GridHeader()this.DefaultStyleKey = typeof(GridHeader);#endregion23#region HeaderText/ / Identifies the HeaderText dependency property./ public static readonly DependencyProperty H

7、eaderTextProperty = DependencyProperty.Register(HeaderText, typeof(string), typeof(GridHeader), null);29/ / Gets or sets the HeaderText possible Value of the int object./ public string HeaderTextget return (string)GetValue(HeaderTextProperty); 2.73.set SetValue(HeaderTextProperty, value); #endregion

8、 HeaderText3940#region FilterText41/ / Identifies theFilterTextdependencyproperty./ public static readonlyDependencyPropertyFilterTextProperty = DependencyProperty.Register(FilterText, typeof(string), typeof(GridHeader), null);46/ / Gets or sets the FilterText possible Value of the string object./ p

9、ublic string FilterTextget return (string)GetValue(FilterTextProperty); set SetValue(FilterTextProperty, value); #endregion FilterText56#region FieldTextpublic static DependencyProperty FieldTextProperty = DependencyProperty.Register(FieldText, typeof(string), typeof(GridHeader), null);public string

10、 FieldTextget return (string)GetValue(FieldTextProperty); set SetValue(FieldTextProperty, value); 6465 #endregion66public override void OnApplyTemplate()base.OnApplyTemplate();StackPanel sp = this.GetTemplateChild(LayoutRoot) as StackPanel;7172 btn_header = sp.Children0 as Button;73 filterText = sp.

11、Children1 as TextBox;74.74if (this.filterText != null)this.filterText.LostFocus += newRoutedEventHandler(filterText_LostFocus);if (this.btn_header != null)this.btn_header.Click += newRoutedEventHandler(header_Click);80.80void header_Click(object sender, RoutedEventArgs e)OnSort(FieldText);85.85void

12、filterText_LostFocus(object sender, RoutedEventArgs e)OnFilter(FieldText, filterText.Text.Trim();90.90第二步,在Themes下添加一個(gè)Generic.xaml,內(nèi)容如下:Generic 這里定義了我們自定義的 ColumnHeader 的展現(xiàn)方式,包括用了一個(gè) Button 和 TextBox。關(guān)于 Generic.xaml 我不是太明白這個(gè)文件,有些人說(shuō)會(huì)自動(dòng)產(chǎn)生這個(gè)文件,但 是我的機(jī)器上并沒(méi)有這個(gè)文件,我是手工添加的,現(xiàn)建一個(gè) themes 下建一個(gè) SilverLight 用戶控件,然后

13、用上面的內(nèi)容替換掉自動(dòng)生成的內(nèi)容,并且去掉Generic.xaml.cs 的 public Generic()里面的代碼。第三步,在DataGrid頁(yè)面上添加Resources。如下:注意,要在文件頭部加上 xmlns:my=clr-namespace:SilverlightDemoApp;assembly=SilverlightDemoApp就是這個(gè) my:GridHeader 的 namespace 和 assembly 信息。不然就找不到 my:GridHeader 的了。第四步,實(shí)現(xiàn) DataGrid 所在頁(yè)面的代碼。這里主要寫一下修改的地方xaml 中的 DataGrid 部分。da

14、 ta:Da taGridTempla teColumn Header=操作在獲取數(shù)據(jù)之后,對(duì)DataGrid的Header進(jìn)行了處理。新增了 CreateHeader() 這個(gè)方法。private void client_GetProductsPagingCompleted(object sender, GetProductsPagingCompletedEventArgs e) TOC o 1-5 h z if (e.Error = null)products_list = e.Result.ToList();dgData.ItemsSource = products_list;if (p

15、ager = null)5.pager = new Pager(e.TotalPage, 2);pager.Click += new Pager.PagerButtonClick(pager_Click); pager.PageIndex = 1;spPager.Children.Clear(); spPager.Children.Add(pager);/ 第一次時(shí)創(chuàng)建 GridHeaderCreateHeader();elseMessageBox.Show(e.Error.Message); canvas.Visibility = Visibility.Collapsed; / 用來(lái)存儲(chǔ) H

16、eaderText 和數(shù)據(jù)庫(kù)字段之間的對(duì)照信息 Dictionary FieldDict = new Dictionary();private void CreateHeader()var v = from p in dgData.Columnswhere p is DataGridBoundColumn select p;foreach (DataGridBoundColumn column in v.ToList()string fieldname = column.Binding.Path.Path;Style style_header = Resourcesgrid-header as

17、 Style; column.HeaderStyle = style_header;/本來(lái)這里我是希望可以直接設(shè)置Header的FieldText為字段名的, 但是卻找不到什么好的方法,/Resources 中這里也不能直接綁定 FieldText 為Binding.Path.Path,因?yàn)檫@里是從 DataGridColumnHeader 來(lái)的,/Da taGridColumnHeader只有Con tent,而找不到對(duì)應(yīng)的綁定信息。 /另外,通過(guò)訪問(wèn)Style的Setter,我也只能獲取到ControlTemplate, 但是 ControlTemplate 下面的內(nèi)容,也就是 my:Gr

18、idHeader 取怎么也獲取 不到,/如果有高人知道,請(qǐng)指教一下。謝謝/所以,沒(méi)辦法,只能暫時(shí)把字段名和HeaderText都保存到Dictionary 里面,供后面使用FieldDict.Add(column.Header.ToString(), fieldname);private void GridHeader_Loaded(object sender, RoutedEventArgs e)GridHeader header = sender as GridHeader; header.OnFilter += newGridHeader.FilterTextEvent(header_OnFilter);header.OnSort += new GridHeader.HeaderClickEvent(header_OnSort);string fieldtext;/ 在這里,我們對(duì) Header 的 FieldText 設(shè)置為字段名。/ 如果能在這里獲取到當(dāng)前的 Column 的話,前面就不要用 Dictionary 了,但是這里我仍然不知道該怎么獲取到當(dāng)前的 Column。if (FieldDict.TryGetValue(header.HeaderText, out fieldtext) header.FieldText = fi

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論