Silverlight 查询DataGrid 中匹配项 ,后台改变选中行颜色
来源:互联网 发布:java系统日志管理 编辑:程序博客网 时间:2024/05/19 15:40
需求:根据关键字(参会人号码或名称)查找参会人,在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 };
搜索按钮事件代码,简单的进行了实现,还可以再优化下,达到更优效果
private void btnSeach_Click(object sender, RoutedEventArgs e) { string txtPhoneno = txtSeach.Text.Trim(); if (txtPhoneno != "") { var collection = dataGrid1.ItemsSource as ObservableCollection<ConfMemberModel>; start: if (next == null) { next = collection.FirstOrDefault<ConfMemberModel>((cmm) => { if (cmm.Phoneno.Contains(txtPhoneno) || cmm.Data.Contains(txtPhoneno)) { dataGrid1.SelectedItem = cmm; dataGrid1.Columns.ToList().ForEach( (dgc) => { FrameworkElement fwElement = dgc.GetCellContent(cmm); SetRowBG(fwElement, new SolidColorBrush(color)); } ); return true; } return false; }); if (next != null) { list.Add(next); } else { var messageBox = new cwConfirmBox(); messageBox.Show("未找到号码" + txtPhoneno); return; } } else { next = collection.FirstOrDefault<ConfMemberModel>((cmm) => { if ((cmm.Phoneno.Contains(txtPhoneno) || cmm.Data.Contains(txtPhoneno)) && !list.Contains(cmm)) { dataGrid1.SelectedItem = cmm; dataGrid1.Columns.ToList().ForEach( (dgc) => { FrameworkElement fwElement = dgc.GetCellContent(cmm); SetRowBG(fwElement, new SolidColorBrush(color)); } ); return true; } else { dataGrid1.Columns.ToList().ForEach( (dgc) => { FrameworkElement fwElement = dgc.GetCellContent(cmm); SetRowBG(fwElement, null); } ); } return false; }); if (next != null) { list.Add(next); } else { list.Clear(); goto start; } } } else { var messageBox = new cwConfirmBox(); messageBox.Show("请输入号码!"); return; } }
/// <summary> /// 设置行背景 /// </summary> /// <param name="b"></param> void SetRowBG(FrameworkElement fwElement, Brush brush) { DependencyObject dpObject = VisualTreeHelper.GetParent(fwElement); if (dpObject.GetType() == typeof(Grid)) { var grid = dpObject as Grid; grid.Background = brush; return; } else { SetRowBG(dpObject as FrameworkElement, brush); } }
原理很简单,就是把所有列的背景进行了改变,此方法对列较少的datagird还行,多了也没测试,不知道性能会有多大影响,水平有限,暂时想到的这样操作,希望有大牛能够指点下。
- Silverlight 查询DataGrid 中匹配项 ,后台改变选中行颜色
- silverlight datagrid 改变颜色
- WPF DataGrid 选中行颜色不改变问题
- DataGrid中鼠标单击行改变颜色
- DataGrid中鼠标单击行改变颜色
- DataGrid中鼠标单击行改变颜色
- DataGrid 行改变颜色
- silverlight 中datagrid不能选中行的问题
- silverlight中颜色转换,实现DataGrid 行不同颜色显示
- 改变选中行的颜色
- WPF DataGrid ListView 等等 改变 选中行 颜色;以及 不变的原因
- DataGrid中鼠标点击、移动后行颜色改变效果
- easyui datagrid中动态改变某行的颜色
- silverlight中如何选中datagrid中一行并处理该行
- 修改DataGrid选中颜色
- winform中GridView控件在选中一行后改变选中行的背景颜色
- 在wpf datagrid中,想要根据一个条件来改变datagrid行的背景颜色
- WP--listBox选中项颜色改变
- php过滤提交数据,防止sql注入攻击
- ubuntu12.04 svn搭建 关联redmine
- c# 添加DLL 直接转到定义
- Deep Learning(深度学习)学习笔记整理系列之(二)
- Deep Learning(深度学习)学习笔记整理系列之(三)
- Silverlight 查询DataGrid 中匹配项 ,后台改变选中行颜色
- Poj 3233
- .NET开发必备网址
- Deep Learning(深度学习)学习笔记整理系列之(四)
- Deep Learning(深度学习)学习笔记整理系列之(五)
- cf ***
- mysql_安装到最后一步时,start_service + MySQL GUI Tools 使用简介
- Deep Learning(深度学习)学习笔记整理系列之(六)
- [leetcode刷题系列]3Sum