Silverlight 查询DataGrid 中匹配项 ,后台改变选中行颜色

需求:根据关键字(参会人号码或名称)查找参会人,在datagird 中高亮显示

界面:我在界面上增加了一个文本框和按钮,进行查找操作

操作说明:

根据关键字进行搜索:输入关键字 点击查找,如果找到 以蓝色背景显示整行数据(坑爹的项目经理非让用这个颜色,一点都不好看偷笑),找不到进行提示;继续点击查找 寻找下一个匹配项

上代码:

声明了三个变量,next 存储下一个匹配项,list 存储所有匹配项,color 为行背景色

 

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

 

搜索按钮事件代码,简单的进行了实现,还可以再优化下,达到更优效果大笑

 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还行,多了也没测试,不知道性能会有多大影响水平有限,暂时想到的这样操作,希望有大牛能够指点下。大笑

 

 

 

posted @ 2013-08-16 19:12  ClrsDream  阅读(2569)  评论(0编辑  收藏  举报