在应用系统中用户往往会有这样的需求,将表格中检索到的数据进行高亮显示。本文主要讲解如何在C1DataGrid/C1FlexGrid for WPF 控件中实现该功能。
首先,在UI中添加以下XAML代码:
<Grid> <Grid.RowDefinitions > <RowDefinition Height="40" /> <RowDefinition Height="30"/> <RowDefinition Height="Auto" /> <RowDefinition Height="30"/> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Button Content="Find" Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="75" Grid.Row="0" /> <TextBox Height="24" HorizontalAlignment="Left" Margin="148,9,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" /> <TextBlock Height="23" HorizontalAlignment="Center" Margin="10,10,0,0" Name="textBlock1" Text="C1Flexgrid" VerticalAlignment="Center" Grid.Row="1" FontSize="15" FontWeight="Bold" /> <c1:C1FlexGrid Name="c1FlexGrid1" Grid.Row="2"/> <TextBlock Height="23" HorizontalAlignment="Center" Margin="10,10,0,0" Name="textBlock2" Text="C1DataGrid" VerticalAlignment="Center" Grid.Row="3" FontSize="15" FontWeight="Bold" /> <c1:C1DataGrid Grid.Row="4" Name="c1DataGrid1" /> </Grid>
然后,我们需要创建一个自定义的CellFactory类型,定义一个包含字符串类型参数的构造函数,并重写CreateCellContent方法,代码如下:
public class MyCellFactory : C1.WPF.FlexGrid.CellFactory { string _text; public MyCellFactory() { } public MyCellFactory(string text) { _text = text; } public override void CreateCellContent(C1.WPF.FlexGrid.C1FlexGrid grid, Border bdr, C1.WPF.FlexGrid.CellRange range) { base.CreateCellContent(grid, bdr, range); if (range.Row >= grid.Rows[0].Index && range.Column >= grid.Columns[0].Index) { //Fetch text from TextBlock var tb = bdr.Child as TextBlock; if (tb != null) { if (tb.Text.Contains(_text)) { //Call method from static class to change text Foreground Range.Find(_text, tb); } } } } }
接着,我们创建一个静态类,其中包含Find方法,代码如下:
public static class Range { public static void Find(string text, TextBlock _tb) { var tb = _tb as TextBlock; var si = tb.Text.IndexOf(text); // TextPointer to the position indicated by the specified offset // from the beginning of the current TextPointer var sp = tb.ContentStart.GetPositionAtOffset(si + 1); var ep = tb.ContentStart.GetPositionAtOffset(si + text.Length + 1); //Change ForeGround and FontWeight of the specified TextRange TextRange tr = new TextRange(sp, ep); tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red); tr.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold); } }
完成以上工作之后,我们需要在用户输入检索关键字并点击按钮之后,将自定义的CellFactory类型设置给C1FlexGrid的CellFactory属性,代码如下:
string _text = string.Empty; button1.Click += (s, e) => { _text = this.textBox1.Text; c1FlexGrid1.CellFactory = new MyCellFactory(_text); };
以上就是在C1FlexGrid中实现高亮显示检索结果的全部代码。然而,C1DataGrid中没有提供CellFactory的概率,
所以,我们需要使用LoadedCellPresenter事件来捕获TextBlock,并把它以及检索文本传递到Range.Fand()方法中。
为了在每次用户输入检索文本之后都能高亮显示检索结果,我们需要在按钮的单击事件中刷新表格,代码如下:
c1DataGrid1.LoadedCellPresenter += (s, e) => { //Get TextBlock using DataGridCellPresenter var presenter = ((C1.WPF.DataGrid.DataGridCellPresenter)(e.Cell.Presenter)); var textblock = ((System.Windows.Controls.TextBlock)(((System.Windows.FrameworkElement)(presenter.Content)))); if (textblock != null && _text != null) { if (textblock.Text.Contains(_text)) { //Call method from static class to change text Foreground Range.Find(_text, textblock); } } }; string _text = string.Empty; button1.Click += (s, e) => { _text = this.textBox1.Text; //refresh both Grids each time button is clicked for a new search c1FlexGrid1.CellFactory = new MyCellFactory(_text); c1DataGrid1.Refresh(); };
运行程序,在文本框中输入 Web 和 Webb 测试,结果如下:
源码下载:VS2010 + C1 Studio for WPF
Wpf_C1Flex_TextForeColor.zip (10.19 kb)
葡萄城控件产品网站:http://www.gcpowertools.com.cn/
葡萄城技术支持论坛:http://gcdn.grapecity.com/
相关推荐
在ComponentOne 2012V3中,开发人员的交互界面和提示信息都是中文,包括菜单和对话框等交互界面,在设计时控件各对象的方法和属性的说明,以及在Visual Studio 环境中的 API 智能提示等。ComponentOne 2012V3还针对...
asdhjkashdkjahsdhklashfkhakgfk afasfasf
c1FlexGrid控件超链接可以实现对列表信息的需要链接的需求
C1控件使用手册,比较全面,中文版的,对于要学习C1控件的童鞋,很适用!
C1FlexGrid中实现类似Excel单元格计算的功能
ComponentOne FlexGrid for Windows Forms的详细教程,包括基本设置、设计时支持、设置和编辑单元格、概述和汇总数据、数据过滤、打印等操作的详细说明和代码示例,并给出了数十个使用经验和技巧。
C1.Win.C1FlexGrid.2.是一个很好用的工具
vsflexgrid 官方实例 ComponentOne Studio.NET C1flexgrid 包括其他的控件官方实例包含vsflexgrid ComponentOne C1flexgrid vb.net C#
c1FlexGrid的下拉,列的类型,单元格和间行颜色等
vs2005开发 在datagridview功能上增加了合并单元格,合计,小计 等功能。
C1Flex表格
c1FlexGrid 中文帮助文档, 从componentone英文例子翻译而来。
C1FlexGrid绘制柱形图,VB.NET实现。
C1.CF.C1FlexGrid.3.dll
通过itemrender实现flex datagrid中显示序列号
jquery flexgrid demo & source code
c1中的FLEXGRID控件的大部分功能,非常实用和简洁。
flexgrid表格控件jsp版本,经过修改,可以在项目中进行引用,界面比较好看~
一个好用的flexgrid