Silverlight 查詢DataGrid 中匹配項 ,後臺改變選中行顏色

需求:根據關鍵字(參會人號碼或名稱)查找參會人,在datagird 中高亮顯示ide

界面:我在界面上增長了一個文本框和按鈕,進行查找操做性能

操做說明:測試

根據關鍵字進行搜索:輸入關鍵字 點擊查找,若是找到 以藍色背景顯示整行數據(坑爹的項目經理非讓用這個顏色,一點都很差看偷笑),找不到進行提示;繼續點擊查找 尋找下一個匹配項優化

上代碼:spa

聲明瞭三個變量,next 存儲下一個匹配項,list 存儲全部匹配項,color 爲行背景色code

 

ConfMemberModel next = null;
List<ConfMemberModel> list = new List<ConfMemberModel>();
Color color = new Color() { A = 255, R = 43, G = 97, B = 237 };
變量

 

搜索按鈕事件代碼,簡單的進行了實現,還能夠再優化下,達到更優效果大笑blog

 1 private void btnSeach_Click(object sender, RoutedEventArgs e)
 2         {
 3             string txtPhoneno = txtSeach.Text.Trim();
 4             if (txtPhoneno != "")
 5             {
 6                 var collection = dataGrid1.ItemsSource as ObservableCollection<ConfMemberModel>;
 7                 
 8                 start: if (next == null)
 9                     {
10                         next = collection.FirstOrDefault<ConfMemberModel>((cmm) =>
11                         {
12 
13                             if (cmm.Phoneno.Contains(txtPhoneno) || cmm.Data.Contains(txtPhoneno))
14                             {
15                                 dataGrid1.SelectedItem = cmm;
16 
17                                 dataGrid1.Columns.ToList().ForEach(
18                                         (dgc) =>
19                                         {
20                                             FrameworkElement fwElement = dgc.GetCellContent(cmm);
21                                             SetRowBG(fwElement, new SolidColorBrush(color));
22                                         }
23 
24                                     );
25                                 return true;
26                             }
27 
28                             return false;
29                         });
30 
31                         if (next != null)
32                         {
33                             list.Add(next);
34                         }
35                         else
36                         {
37                             var messageBox = new cwConfirmBox();
38                             messageBox.Show("未找到號碼" + txtPhoneno);
39                             return;
40                         }
41                     }
42                     else
43                     {
44                         next = collection.FirstOrDefault<ConfMemberModel>((cmm) =>
45                         {
46 
47                             if ((cmm.Phoneno.Contains(txtPhoneno) || cmm.Data.Contains(txtPhoneno)) && !list.Contains(cmm))
48                             {
49                                 dataGrid1.SelectedItem = cmm;
50 
51                                 dataGrid1.Columns.ToList().ForEach(
52                                         (dgc) =>
53                                         {
54                                             FrameworkElement fwElement = dgc.GetCellContent(cmm);
55                                             SetRowBG(fwElement, new SolidColorBrush(color));
56                                         }
57 
58                                     );
59                                 return true;
60                             }
61                             else
62                             {
63                                 dataGrid1.Columns.ToList().ForEach(
64                                         (dgc) =>
65                                         {
66                                             FrameworkElement fwElement = dgc.GetCellContent(cmm);
67                                             SetRowBG(fwElement, null);
68                                         }
69 
70                                     );
71                             }
72                             return false;
73                         });
74 
75                         if (next != null)
76                         {
77                             list.Add(next);
78                         }
79                         else
80                         {
81                             list.Clear();
82                             goto start;
83                         }
84 
85                     }
86             }
87             else
88             {
89                 var messageBox = new cwConfirmBox();
90                 messageBox.Show("請輸入號碼!");
91                 return;
92             }
93         }
按鈕事件
 1         /// <summary>
 2         /// 設置行背景
 3         /// </summary>
 4         /// <param name="b"></param>
 5         void SetRowBG(FrameworkElement fwElement, Brush brush)
 6         {
 7             DependencyObject dpObject = VisualTreeHelper.GetParent(fwElement);
 8 
 9             if (dpObject.GetType() == typeof(Grid))
10             {
11                 var grid = dpObject as Grid;
12 
13                 grid.Background = brush;
14 
15                 return;
16             }
17             else
18             {
19                 SetRowBG(dpObject as FrameworkElement, brush);
20             }
21 
22         }
設置背景

原理很簡單,就是把全部列的背景進行了改變,此方法對列較少的datagird還行,多了也沒測試,不知道性能會有多大影響水平有限,暫時想到的這樣操做,但願有大牛可以指點下。大笑事件

相關文章
相關標籤/搜索