如何在Windows Phone 7中突出显示上下文相关搜索的结果?

我需要根据在windows phone 7的文本框中输入的文本突出显示搜索结果,

在此处输入图像描述

通常的wpf代码在Windows Phone 7中不起作用。 有人说如何在Windows Phone 7中实现这一点

实际上这是我用来填充联系人列表的xaml Listbox,

             

C#代码,

  using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; //using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls; using Microsoft.Phone.UserData; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Collections.ObjectModel; using Microsoft.Phone.Shell; using System.Windows.Controls; using System.Windows.Media.Imaging; using System.Text.RegularExpressions; namespace SmartContactsApp { public partial class MainPage : PhoneApplicationPage { private List
lstAddress = new List
(); public string addressJson = string.Empty; // Constructor public MainPage() { InitializeComponent(); this.Loaded += new RoutedEventHandler(MainPage_Loaded); CreateSecondaryTile(); } /// /// To List all the Contacts. . . /// private void ContactListing() { ContactList.DataContext = null; Contacts cons = new Contacts(); cons.SearchCompleted += new EventHandler(Contacts_SearchCompleted); cons.SearchAsync(contactFilterString.Text, FilterKind.DisplayName, "Contacts"); } /// /// To Fetch All Contacts from Mobile Contacts. . . /// /// /// public void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e) { try { ContactList.DataContext = e.Results; } catch (Exception) { throw; } } private void contactFilterString_TextChanged(object sender, TextChangedEventArgs e) { ContactListing(); } }

如何突出这一点,

提前致谢!

我是在这个帮助下完成的

Xaml代码:

                

c#代码:

  private void contactFilterString_TextChanged(object sender, TextChangedEventArgs e) { // ContactListing(); SearchVisualTree(ContactList); if (contactFilterString.Text == "") { ContactListing(); } } private void SearchVisualTree(Action ContactListing) { SearchVisualTree(ContactList); } private void SearchVisualTree(DependencyObject targetElement) { var count = VisualTreeHelper.GetChildrenCount(targetElement); for (int i = 0; i < count; i++) { var child = VisualTreeHelper.GetChild(targetElement, i); if (child is TextBlock) { textBlock1 = (TextBlock)child; HighlightText(); break; } else { //ContactListing(); SearchVisualTree(child); } } } private void HighlightText() { if (textBlock1 != null) { string text = textBlock1.Text; textBlock1.Text = text; textBlock1.Inlines.Clear(); int index = text.IndexOf(contactFilterString.Text); int lenth = contactFilterString.Text.Length; if (!(index < 0)) { Run run = new Run() { Text = text.Substring(index, lenth), FontWeight = FontWeights.ExtraBold }; run.Foreground = new SolidColorBrush(Colors.Orange); textBlock1.Inlines.Add(new Run() { Text = text.Substring(0, index), FontWeight = FontWeights.Normal }); textBlock1.Inlines.Add(run); textBlock1.Inlines.Add(new Run() { Text = text.Substring(index + lenth), FontWeight = FontWeights.Normal }); textBlock1.FontSize = 30; textBlock1.Foreground = new SolidColorBrush(Colors.Black); } else { //textBlock1.Text = "No Match"; } } } ///  /// To List all the Contacts. . . ///  private void ContactListing() { ContactList.DataContext = null; Contacts cons = new Contacts(); cons.SearchCompleted += new EventHandler(Contacts_SearchCompleted); cons.SearchAsync(contactFilterString.Text, FilterKind.DisplayName, "Contacts"); } ///  /// To Fetch All Contacts from Mobile Contacts. . . ///  ///  ///  public void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e) { try { ContactList.DataContext = e.Results; } catch (Exception) { throw; } } 

谢谢您的帮助!

对于类似的要求,我做了以下事情:

首先,我将以下内容作为ListBox数据模板的一部分

     

然后在后面的代码中:我用来绑定到ListBox的列表定义如下

  public List results { get { return _results; } set { string x = null; foreach (var item in value) { item.text2 = item.text; if (!item.text.StartsWith(" 0) { var match = @query.Trim(); xamlData = Regex.Replace(item.text, match, "" + match + "", RegexOptions.IgnoreCase); } if (xamlData == null) xamlData = item.text; item.text = "
" + xamlData + "
"; } } _results = value; } }

然后在获得搜索结果后,将它们添加到上面的列表中,然后将其设置为ListBox的ItemsSource。

现在是ListBox的关键部分。 添加listbox_SizeChanged事件处理程序,如下所示

 private void listboxMyTimeline_SizeChanged(object sender, SizeChangedEventArgs e) { for (int i = 0; i < listboxMyTimeline.Items.Count; i++) { ListBoxItem lbi2 = (ListBoxItem)(listboxMyTimeline.ItemContainerGenerator.ContainerFromIndex(i)); if (lbi2 != null) { var ob = FindFirstElementInVisualTree(lbi2); var ob2 = FindFirstElementInVisualTree(lbi2); ob.Xaml = ob2.Text; } else { var itm = listboxMyTimeline.Items.ElementAt(i); lbi2 = (ListBoxItem)(listboxMyTimeline.ItemContainerGenerator.ContainerFromItem(itm)); if (lbi2 != null) { var ob = FindFirstElementInVisualTree(lbi2); var ob2 = FindFirstElementInVisualTree(lbi2); ob.Xaml = ob2.Text; } } } } 

FindFirstElementVisualTree方法是这样的

 private T FindFirstElementInVisualTree(DependencyObject parentElement) where T : DependencyObject { var count = VisualTreeHelper.GetChildrenCount(parentElement); if (count == 0) return null; for (int i = 0; i < count; i++) { var child = VisualTreeHelper.GetChild(parentElement, i); if (child != null && child is T) { return (T)child; } else { var result = FindFirstElementInVisualTree(child); if (result != null) return result; } } return null; } 

所以,我正在使用一个隐藏的文本框,我绑定了实际文本的构造XAML代码。 这是因为您无法直接绑定到RichTextBox或Run元素。 然后在Size_changed处理程序中,我将XAMl代码设置为可见的RichTextBox。

我不确定它在多大程度上适合您的需求,您可能需要对上述过程进行许多更改,以使其适合您。

祝好运 :)

更新:

使用代码中的Address类替换我的代码中的Result类。

并用我的列表替换您的列表定义。

在Address类中添加一个名为’xamlCode’的附加属性。 (将item.text2 = item.text行替换为item.xamlCode = item.DisplayName

其余的应该对你很清楚。

如果有任何疑问, 请在这里问 。