【问题标题】:Highlighting Text in WP7 Databound Listbox在 WP7 数据绑定列表框中突出显示文本
【发布时间】:2010-12-21 05:30:46
【问题描述】:

我正在尝试突出显示数据绑定 ListBox 中的文本并突出显示匹配的字符串,就像 Windows Phone 7 上的电子邮件应用程序一样。

搜索按钮拉出一个弹出窗口,在 TextChanged 事件上,我从主列表中过滤并重新设置 DataContext:

private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
{
  results = allContent.Where(
    x => x.Content.Contains(txtSearch.Text)
  ).ToList();

  DataContext = results;
}

那部分效果很好。问题在于突出显示匹配的文本。我尝试在各种事件(Loaded、ItemsChanged)中迭代 ListBoxItems,但它们总是空的。

关于如何在数据绑定 ListItem 的子 TextBox 中突出显示文本有什么想法吗?

【问题讨论】:

    标签: wpf windows-phone-7


    【解决方案1】:

    这是我采用的解决方案:

    private void ResultsText_Loaded(object sender, RoutedEventArgs e)
    {
        var textBlock = sender as TextBlock;
        if (txtSearch.Text.Length > 0 && textBlock.Text.Length > 0)
        {
            BoldText(ref textBlock, txtSearch.Text, Color.FromArgb(255, 254, 247, 71));
        }
    }
    
    public static void BoldText(ref TextBlock tb, string partToBold, Color color)
    {
        string Text = tb.Text;
        tb.Inlines.Clear();
    
        Run r = new Run();
        r.Text = Text.Substring(0, Text.IndexOf(partToBold));
        tb.Inlines.Add(r);
    
        r = new Run();
        r.Text = partToBold;
        r.FontWeight = FontWeights.Bold;
        r.Foreground = new SolidColorBrush(color);
        tb.Inlines.Add(r);
    
        r = new Run();
        r.Text = Text.Substring(Text.IndexOf(partToBold) + partToBold.Length, Text.Length - (Text.IndexOf(partToBold) + partToBold.Length));
        tb.Inlines.Add(r);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-23
      • 2018-10-05
      • 1970-01-01
      • 2015-12-21
      相关资源
      最近更新 更多