回答
我曾經爲WPF應用程序編寫過類似的功能。搜索到的文本應突出顯示DataGrid
的項目。所有你需要的是一個MultiValueConverter
,它將您的項目和搜索文本的文本轉換爲一個新的TextBlock
,其中包含Run
元素與突出顯示的部分。
轉換
轉換器將文本與搜索文本到TextBlock
實例,其中包含以限定的風格比賽Run
元素轉換。在你
public class TextToHighlightedTextConverter : IMultiValueConverter
{
public Style HighlightedTextStyle { get; set; }
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length > 0)
{
if (values.Length > 1)
{
var text = values[0] as string;
var searchText = values[1] as string;
if (!string.IsNullOrEmpty(text) && !string.IsNullOrEmpty(searchText))
{
var textParts = GetSplittedText(text, searchText);
var textBlock = new TextBlock();
foreach (string textPart in textParts)
{
textBlock.Inlines.Add(textPart.Equals(searchText, StringComparison.OrdinalIgnoreCase)
? new Run(textPart) {Style = HighlightedTextStyle ?? new Style()}
: new Run(textPart));
}
return textBlock;
}
}
return values[0];
}
return null;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
private IEnumerable<string> GetSplittedText(string text, string searchText)
{
IList<string> textParts = new List<string>();
if (string.IsNullOrEmpty(searchText))
{
if (text.Length > 0)
{
textParts.Add(text);
}
}
else
{
while (text.Length > 0)
{
int searchIndex = text.IndexOf(searchText, StringComparison.OrdinalIgnoreCase);
if (searchIndex > -1)
{
if (searchIndex > 0)
{
string textInFrontOfMatch = text.Substring(0, searchIndex);
textParts.Add(textInFrontOfMatch);
}
textParts.Add(text.Substring(searchIndex, searchText.Length));
text = text.Remove(0, searchIndex + searchText.Length);
}
else
{
textParts.Add(text);
text = string.Empty;
}
}
}
return textParts;
}
}
轉換器定義XAML文件
在XAML文件中定義你的轉換器,並設置應該用於比賽的風格。
<Converters:TextToHighlightedTextConverter x:Key="TextToHighlightedTextConverter">
<Converters:TextToHighlightedTextConverter.HighlightedTextStyle>
<Style TargetType="{x:Type Run}">
<Setter Property="Background" Value="Orange" />
</Style>
</Converters:TextToHighlightedTextConverter.HighlightedTextStyle>
</Converters:TextToHighlightedTextConverter>
爲您的列表框
轉換器的使用您可以定義爲您的ListBox
的項DataTemplate
。此DataTemplate
使用ContentPresenter
,其內容將由定義的轉換器設置。
<ListBox ItemsSource={Binding YourItemsSource}>
<ListBox.ItemsTemplate>
<DataTemplate>
<ContentPresenter>
<ContentPresenter.Content>
<MultiBinding Converter="{StaticResource TextToHighlightedTextConverter}">
<MultiBinding.Bindings>
<Binding />
<Binding Path="YourSearchTextSource" />
</MultiBinding.Bindings>
</MultiBinding>
</ContentPresenter.Content>
</ContentPresenter>
</DataTemplate>
</ListBox.ItemsTemplate>
</ListBox>
使用CollectionViewSource其過濾和搜索非常有用:
Reffer: http://www.geoffhudik.com/tech/2010/10/14/wp7-in-app-searching-filtering.html
謝謝!這篇文章幫了我很多。我在我的項目中嘗試了它。 – user1538895 2012-07-22 06:58:58
@ user1538895:如果它可以幫助你..請放棄投票回答。:) – Mahantesh 2012-07-23 10:58:08
我不能。因爲我的聲望小於15.我是新的stackoverflow。抱歉 – user1538895 2012-07-24 11:08:26
- 1. WP7:用瓦片構建應用程序
- 2. 我應該如何構建這個應用程序?
- 3. 如何在Marklogic中構建簡單的搜索應用程序?
- 4. 如何從應用程序啓動wp7內置Bing搜索
- 5. 我應該爲arm64構建我的ios應用程序嗎?
- 6. 在應用搜索在WP7應用程序
- 7. 我應該如何構建我的節點/ express/mongodb應用程序?
- 8. 我應該如何在Django中構建我的靜態應用程序文件?
- 9. ASP.NET MVC應用程序搜索,排序和過濾
- 10. 如何在發佈模式下構建WP7應用程序?
- 11. 食物和餐廳搜索/過濾應用程序的數據庫表結構
- 12. 我應該如何構建我的火力模式以允許搜索功能
- 13. 我應該如何使用Ruby on Rails構建此應用程序?
- 14. 我應該如何創建可更新的應用程序?
- 15. 通過彈性搜索搜索後應用過濾器
- 16. 我應該如何爲移動應用程序構建服務器
- 17. 我應該如何構建視頻流式網絡應用程序?
- 18. 我應該如何搜索這個json結構?
- 19. 如何過濾系統應用程序?
- 20. 彈性搜索:使用過濾器,應該布爾查詢
- 21. 如何使用Lucene.Net&.Net構建搜索引擎(Web應用程序)?
- 22. 如何開始我的應用程序的市場搜索
- 23. 我應該使用Redux創建我的ReactNative應用程序嗎?
- 24. 我應該在ASP.NET MVC中構建我的下一個Web應用程序嗎?
- 25. 通過我的應用程序搜索其他設備
- 26. 在WP7中構建應用程序欄的正確方法
- 27. 如何從我的應用程序在Android中搜索YouTube
- 28. 如何在我的應用程序中搜索配方列表
- 29. 如何在從iPad搜索時搜索iPhone應用程序
- 30. 我應該如何通過字符串搜索字符序列,如「x * y」?
謝謝。但我覺得CollectionViewSource會更好地滿足我的需求。 – user1538895 2012-07-22 07:00:03