让Silverlight AutoCompleteBox控件支持多属性筛选

时间:2022-06-09
本文章向大家介绍让Silverlight AutoCompleteBox控件支持多属性筛选,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

首先我们先建立一个实体类并创建它的集合用于绑定控件的ItemSource属性,这个实体类有两个属性,如下:

   1:      public class AUser
   2:      {
   3:          public string Name { set; get; }
   4:          public string Age { set; get; }
   5:      }

编写XAML如下:

<sdk:AutoCompleteBox  Height="28" Name="autoCompleteBox1" ValueMemberPath="Age" >
    <sdk:AutoCompleteBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock  Text="{Binding Name}"/>
                        <TextBlock Text="{Binding Age}"/>
                    </StackPanel>
                </DataTemplate>
    </sdk:AutoCompleteBox.ItemTemplate>
</sdk:AutoCompleteBox>

大家都知道, 一般情况下AutoCompleteBox控件只能筛选绑定项中的一个属性,(及ValueMemberPath在本例中只能设置为Name或Age),并且ValueMemberPath属性中的路径既是筛选属性路径又是选定项的结果属性路径。

接下来我们扩展它,使它能支持多属性路径,并能指定输出结果属性路径。我的解决思路是利用附加属性的力量达到目标,代码如下:

   1:   public static class AutoCompleteBoxHelper
   2:      {

3: private static void OnIsOtherPathChanged(DependencyObject obj,

DependencyPropertyChangedEventArgs e)

   4:          {
   5:              var acb = obj as AutoCompleteBox;
   6:              if (!string.IsNullOrEmpty(e.NewValue.ToString()))
   7:              {
   8:                  acb.ItemFilter = (s,item)=>{
   9:                      var objType = item.GetType();
  10:                      string path = GetOtherPaths(acb);
  11:                      var pathes = path.Split(',');
  12:                      bool result = false;         
  13:                      foreach (string p in pathes)
  14:                      {
  15:                          var propertyInfo = objType.GetProperty(p);
  16:                          string value = propertyInfo.GetValue(item, null).ToString();
  17:                          result |= value.Contains(s);
  18:                      }
  19:                      return result;
  20:                  };
  21:              }
  22:              else
  23:                 acb.ItemFilter = null;
  24:          }
  25:   
  26:          public static string GetOtherPaths(DependencyObject obj)
  27:          {
  28:              return (string)obj.GetValue(OtherPathsProperty);
  29:          }
  30:   
  31:          public static void SetOtherPaths(DependencyObject obj, string value)
  32:          {
  33:              obj.SetValue(OtherPathsProperty, value);
  34:          }
  35:   
  36:          public static readonly DependencyProperty OtherPathsProperty =
  37:             DependencyProperty.RegisterAttached("OtherPaths", typeof(string),
  38:             typeof(AutoCompleteBoxHelper),
  39:             new PropertyMetadata("", OnIsOtherPathChanged));
  40:      }

OtherPaths属性就是一个支持多属性路径的属性(如”Name,Age”),并且指定了OtherPaths后ValueMemeberPath的筛选作用就不再工作了,而只保留了选定结果项属性路径的功能。

最后要叫它工作:

<sdk:AutoCompleteBox Height="28" Name="autoCompleteBox1" ValueMemberPath="Age"

loc:AutoCompleteBoxHelper.OtherPaths="Age,Name">

运行结果: