How to Highlight the result from context sensitive search in windows phone 7?

前端 未结 2 1027
栀梦
栀梦 2021-01-02 10:51

I need to to Highlight the search result according to the text that is entered in the text box in windows phone 7,

2条回答
  •  余生分开走
    2021-01-02 10:53

    For a similar kind of requirement, I did the following:

    First I took the following as part of my ListBox data template

    
        
        
    
    

    Then in the code behind: The list which I am using to bind to the ListBox is defined as follows

     public List results {
            get
            {  
                return _results;
            }
            set
            {
                string x = null;
                foreach (var item in value)
                {
                    item.text2 = item.text;
                    if (!item.text.StartsWith(""))
                    {
                        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, "" + match + "", RegexOptions.IgnoreCase);
                        }
                        if (xamlData == null)
                            xamlData = item.text;
                        item.text = "
    " + xamlData + "
    "; } } _results = value; } }

    Then after getting the search results, add those to the above list and then setting it as the ItemsSource for the ListBox.

    Now comes the critical part of the ListBox. Add a listbox_SizeChanged event handler as shown below

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

    Where the FindFirstElementVisualTree method is like this

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

    So, I am taking a hidden text box to which I am binding the actual text's constructed XAML code. This is because you cannot directly bind to the RichTextBox or Run elements. Then In the Size_changed handler, I am setting that XAMl code to the visible RichTextBox.

    I am not sure to what extent it suits your needs, you may need to make many changes to the above process inorder to make it work for you.

    Good luck :)

    UPDATE:

    Replace the Result class in my code with the Address class in your code.

    And Replace your List definition with my List.

    Add an additional property called 'xamlCode' in your Address class. (Replace the item.text2 = item.text line as item.xamlCode = item.DisplayName

    And the rest should be clear for you.

    Still if any doubts, Ask here.

提交回复
热议问题