前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >让Silverlight AutoCompleteBox控件支持多属性筛选

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

作者头像
yoyofx
发布2018-09-05 11:01:06
3700
发布2018-09-05 11:01:06
举报
文章被收录于专栏:ASP.NETCoreASP.NETCoreASP.NETCore

首先我们先建立一个实体类并创建它的集合用于绑定控件的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">

运行结果:

a1
a1
a2
a2
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2011-06-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档