【Telerik】查询控件<telerik:RadMaskedTextBox>的使用

在SilverLight项目中,实现模糊查询,并将值绑定到列表中,使用了Telerik中的<telerik:RadMaskedTextBox>控件。

要先添加命名空间的引用: xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"

并且项目要添加引用:

 

xaml页面代码:

 1  <!-- 检索-->
 2                     <Border>
 3                         <Grid HorizontalAlignment="Left" Width="150">
 4                             <Grid.ColumnDefinitions>
 5                                 <ColumnDefinition Width="Auto"></ColumnDefinition>
 6                                 <ColumnDefinition></ColumnDefinition>
 7                             </Grid.ColumnDefinitions>
 8                             <Image  Grid.Column="0" Source="/MASP;Component/Images/Find.png" Stretch="Uniform" Margin="4"></Image>
 9                             <telerik:RadMaskedTextBox  x:Name="mask_treat" Grid.Column="1" Margin="3" BorderThickness="0" 
10 EmptyContent="输入关键字进行检索" MaskType="None" VerticalAlignment="Center" ValueChanged="mask_treat_ValueChanged"></telerik:RadMaskedTextBox>
11                         </Grid>
12                     </Border>

xaml.cs代码:文本内容发生改变事件ValueChanged

 1  private void mask_treat_ValueChanged(object sender, Telerik.Windows.RadRoutedEventArgs e)
 2         {
 3             try
 4             {
 5                 string value = mask_treat.MaskedText == null ? "" : mask_treat.MaskedText.ToString().Trim();
 6 
 7                 IList<GridViewDataColumn> ViewDataColumns = new List<GridViewDataColumn>();
 8                 ViewDataColumns.Add(Rgv_treatlist.Columns[2] as GridViewDataColumn);
 9                 ViewDataColumns.Add(Rgv_treatlist.Columns[3] as GridViewDataColumn);
10                 ViewDataColumns.Add(Rgv_treatlist.Columns[4] as GridViewDataColumn);
11                 CustomFilterDescriptor FilterDescriptor = new CustomFilterDescriptor(ViewDataColumns);
12                 FilterDescriptor.FilterValue = value;
13                 Rgv_treatlist.FilterDescriptors.Clear();
14                 Rgv_treatlist.FilterDescriptors.Add(FilterDescriptor);
15             }
16             catch (Exception ex)
17             {
18                 MessageBox.Show(ex.Message);
19             }
20         }

CustomFilterDescriptor类代码:

  1 using Telerik.Windows.Controls;
  2 using Telerik.Windows.Data;  
  3 
  4 public class CustomFilterDescriptor : FilterDescriptorBase
  5     {
  6         private readonly CompositeFilterDescriptor compositeFilterDesriptor;
  7         private static readonly ConstantExpression TrueExpression = System.Linq.Expressions.Expression.Constant(true);
  8         private string filterValue;
  9 
 10         public CustomFilterDescriptor(IEnumerable<Telerik.Windows.Controls.GridViewColumn> columns)
 11         {
 12             this.compositeFilterDesriptor = new CompositeFilterDescriptor();
 13             this.compositeFilterDesriptor.LogicalOperator = FilterCompositionLogicalOperator.Or;
 14 
 15             foreach (GridViewDataColumn column in columns)
 16             {
 17                 this.compositeFilterDesriptor.FilterDescriptors.Add(this.CreateFilterForColumn(column));
 18             }
 19         }
 20 
 21         public string FilterValue
 22         {
 23             get
 24             {
 25                 return this.filterValue;
 26             }
 27             set
 28             {
 29                 if (this.filterValue != value)
 30                 {
 31                     this.filterValue = value;
 32                     this.UpdateCompositeFilterValues();
 33                     this.OnPropertyChanged("FilterValue");
 34                 }
 35             }
 36         }
 37 
 38         protected override System.Linq.Expressions.Expression CreateFilterExpression(ParameterExpression parameterExpression)
 39         {
 40             if (string.IsNullOrEmpty(this.FilterValue))
 41             {
 42                 return TrueExpression;
 43             }
 44             try
 45             {
 46                 return this.compositeFilterDesriptor.CreateFilterExpression(parameterExpression);
 47             }
 48             catch
 49             {
 50             }
 51 
 52             return TrueExpression;
 53         }
 54 
 55         private IFilterDescriptor CreateFilterForColumn(GridViewDataColumn column)
 56         {
 57             FilterOperator filterOperator = GetFilterOperatorForType(column.DataType);
 58             FilterDescriptor descriptor = new FilterDescriptor(column.UniqueName, filterOperator, this.filterValue);
 59             descriptor.MemberType = column.DataType;
 60 
 61             return descriptor;
 62         }
 63 
 64 
 65         private static FilterOperator GetFilterOperatorForType(Type dataType)
 66         {
 67             return dataType == typeof(string) ? FilterOperator.Contains : FilterOperator.IsEqualTo;
 68         }
 69 
 70         private void UpdateCompositeFilterValues()
 71         {
 72             foreach (FilterDescriptor descriptor in this.compositeFilterDesriptor.FilterDescriptors)
 73             {
 74                 object convertedValue = DefaultValue(descriptor.MemberType);
 75 
 76                 try
 77                 {
 78                     convertedValue = Convert.ChangeType(this.FilterValue, descriptor.MemberType, null);
 79                 }
 80                 catch
 81                 {
 82                 }
 83 
 84                 if (!descriptor.MemberType.IsAssignableFrom(typeof(DateTime)))
 85                 {
 86                     descriptor.Value = convertedValue; ;
 87 
 88                 }
 89             }
 90         }
 91 
 92         private static object DefaultValue(Type type)
 93         {
 94             if (type.IsValueType)
 95             {
 96                 return Activator.CreateInstance(type);
 97             }
 98 
 99             return null;
100         }
101     }

记录完成。

 

posted @ 2016-06-23 11:16  库拉丽秋  阅读(1092)  评论(0编辑  收藏  举报