8

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

在此处输入图像描述

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

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

    <ListBox Name="ContactList" ItemsSource="{Binding}" Margin="14,85,14,28" Foreground="White" SizeChanged="ContactList_SizeChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Border BorderThickness="2" HorizontalAlignment="Left" VerticalAlignment="Center" BorderBrush="{StaticResource PhoneAccentBrush}" >
                            <Image Source="{Binding Converter={StaticResource ContactPictureConverter}}" Width="48" Height="48" Stretch="Fill" Name="img1" />
                        </Border>
                        <TextBlock Name="ContactResults" Text="{Binding Path=DisplayName, Mode=OneWay}" FontSize="{StaticResource PhoneFontSizeExtraLarge}" Margin="18,8,0,0" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <TextBox Name="contactFilterString" Margin="0,0,0,528" TextChanged="contactFilterString_TextChanged" />

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<Address> lstAddress = new List<Address>();
    public string addressJson = string.Empty;

    // Constructor
    public MainPage()
    {
        InitializeComponent();

        this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        CreateSecondaryTile();
    }

    /// <summary>
    /// To List all the Contacts. . .
    /// </summary>
    private void ContactListing()
    {
        ContactList.DataContext = null;
        Contacts cons = new Contacts();
        cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);
        cons.SearchAsync(contactFilterString.Text, FilterKind.DisplayName, "Contacts");
    }

    /// <summary>
    /// To Fetch All Contacts from Mobile Contacts. . .
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    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();
    }
    }

如何在此突出显示,

提前致谢!

4

2 回答 2

5

我在这个的帮助下做到

Xml代码:

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <ListBox Name="ContactList" Margin="14,85,14,28" Foreground="White">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding DisplayName}" Width="450" TextWrapping="Wrap" FontSize="24" Visibility="Collapsed"/>
                        <RichTextBox Width="450" FontSize="24" Foreground="#FFFFFF"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <TextBox Name="contactFilterString" Margin="0,0,0,528" TextChanged="contactFilterString_TextChanged" />
    </Grid>

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";

            }
        }

    }

     /// <summary>
    /// To List all the Contacts. . .
    /// </summary>
    private void ContactListing()
    {
        ContactList.DataContext = null;
        Contacts cons = new Contacts();
        cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);
        cons.SearchAsync(contactFilterString.Text, FilterKind.DisplayName, "Contacts");
    }

    /// <summary>
    /// To Fetch All Contacts from Mobile Contacts. . .
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
    {
        try
        {

            ContactList.DataContext = e.Results;

        }
        catch (Exception)
        {
            throw;
        }
    }

谢谢您的帮助!

于 2012-12-28T06:44:28.610 回答
1

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

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

<StackPanel Width="450">
    <TextBlock Text="{Binding text}" Width="450" TextWrapping="Wrap" FontSize="24" Visibility="Collapsed"/>
    <RichTextBox Width="450" FontSize="24" Foreground="#FFFFFF"/>
</StackPanel>

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

 public List<Result> results {
        get
        {  
            return _results;
        }
        set
        {
            string x = null;
            foreach (var item in value)
            {
                item.text2 = item.text;
                if (!item.text.StartsWith("<Section"))
                    if(!item.text.Contains("</Run>"))
                {
                    String xamlData = null;
                    var regx = new Regex(@query.Trim(), RegexOptions.IgnoreCase);
                    var matcches = regx.Matches(item.text);
                    x += matcches.Count;
                    if (matcches.Count > 0)
                    {
                        var match = @query.Trim();
                        xamlData = Regex.Replace(item.text, match, "<Run Foreground="Blue" FontWeight=\"ExtraBold\">" + match + "</Run>", RegexOptions.IgnoreCase);
                    }
                    if (xamlData == null)
                        xamlData = item.text;
                    item.text = "<Section xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><Paragraph>" + xamlData + "</Paragraph></Section>";
                }
            }
            _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<RichTextBox>(lbi2);
                var ob2 = FindFirstElementInVisualTree<TextBlock>(lbi2);
                ob.Xaml = ob2.Text;
            }
            else
            {
                var itm = listboxMyTimeline.Items.ElementAt(i);
                lbi2 = (ListBoxItem)(listboxMyTimeline.ItemContainerGenerator.ContainerFromItem(itm));
                if (lbi2 != null)
                {
                    var ob = FindFirstElementInVisualTree<RichTextBox>(lbi2);
                    var ob2 = FindFirstElementInVisualTree<TextBlock>(lbi2);
                    ob.Xaml = ob2.Text;
                }
            }
        }
    }

FindFirstElementVisualTree 方法在哪里是这样的

private T FindFirstElementInVisualTree<T>(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<T>(child);
                if (result != null)
                    return result;

            }
        }
        return null;
    }

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

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

祝你好运 :)

更新:

将我代码中的 Result 类替换为代码中的 Address 类。

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

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

其余的对你来说应该很清楚。

如果有任何疑问,请在此处提问

于 2012-12-22T13:11:28.113 回答