【问题标题】:Can a search result TextHighlighter or TextRange be bound to a DataTemplate in UWP XAML?搜索结果 TextHighlighter 或 TextRange 是否可以绑定到 UWP XAML 中的 DataTemplate?
【发布时间】:2020-04-23 14:48:40
【问题描述】:

我有一个绑定到ListViewSearchResult 类。我想要做的是突出显示搜索结果文本中与用户输入的查询匹配的 sn-p。

相关的 XAML 看起来像这样(省略绒毛):

<DataTemplate>
   <StackPanel>
      <!-- Search result -->
      <RichTextBlock>
         <!-- Would this idea work? -->
         <RichTextBlock.TextHighlighters>
            <TextHighlighter>
               <TextHighlighter.Ranges>
                  <!-- Add the bound range here-->
                  <!-- {Binding Range} or text highlighter or something -->
               </TextHighlighter.Ranges>
            </TextHighlighter>
         </RichTextBlock.TextHighlighters>
         <Paragraph>
            <Run Text="{Binding Text}"></Run>
         </Paragraph>
      </RichTextBlock>
   </StackPanel>
</DataTemplate>

我可以添加SearchResult 类中的任何属性,无论是TextHighlighter 还是TextRange。我只是不知道 XAML 语法是否允许插入该值。

我也考虑过在代码中执行此操作,但我确实希望将搜索项模板保留在 XAML 中,而不是将其放在 C# 中。但是,可以执行lvSearchResults.Items[i]... 之类的操作,或者将其放入荧光笔或范围内。我只是暂时想不出正确的方法。

【问题讨论】:

    标签: xaml listview uwp binding


    【解决方案1】:

    如果您打算创建一个本地突出显示的搜索结果列表,您可以尝试这种方式:

    1. 创建搜索结果类
    public class SearchResult
    {
        public string DisplayText { get; set; }
        public string HighlightText { get; set; }
    }
    
    1. 创建UserControl 以显示结果

    SearchResultBlock.xaml

    <Grid>
        <TextBlock x:Name="ResultBlock" TextWrapping="Wrap" MaxLines="2"
                   TextTrimming="CharacterEllipsis"/>
    </Grid>
    

    SearchResultBlock.xaml.cs

    public sealed partial class SearchResultBlock : UserControl
    {
        public SearchResultBlock()
        {
            this.InitializeComponent();
        }
    
    
        public SearchResult Result
        {
            get { return (SearchResult)GetValue(ResultProperty); }
            set { SetValue(ResultProperty, value); }
        }
    
        public static readonly DependencyProperty ResultProperty =
            DependencyProperty.Register("Result", typeof(SearchResult), typeof(SearchResultBlock), new PropertyMetadata(null,new PropertyChangedCallback(Result_Changed
    
        private static void Result_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if(e.NewValue!=null && e.NewValue is SearchResult data)
            {
                var instance = d as SearchResultBlock;
                instance.ResultBlock.Inlines.Clear();
                var sp = data.DisplayText.Split(data.HighlightText);
                instance.ResultBlock.Inlines.Add(new Run { Text = sp.First() });
                instance.ResultBlock.Inlines.Add(new Run { Text = data.HighlightText, Foreground = new SolidColorBrush(Colors.Red) });
                if (sp.Length > 1)
                    instance.ResultBlock.Inlines.Add(new Run { Text = sp.Last() });
            }
        }
    }
    
    1. DataTemplate中使用它
    <DataTemplate x:DataType="SearchResult" x:Key="ResultItemTemplate">
        <SearchResultBlock Result="{Binding}"/>
    </DataTemplate>
    

    通过字符串拆分,创建不同类型的Runs,并将它们合并到TextBlock中。这样也能达到高亮的效果。

    最好的问候。

    【讨论】:

      猜你喜欢
      • 2019-01-17
      • 2018-07-06
      • 2010-10-15
      • 2019-04-13
      • 2020-02-01
      • 1970-01-01
      • 2014-06-06
      • 2012-11-14
      • 2021-06-23
      相关资源
      最近更新 更多