C# 如何突出显示windows phone 7中上下文相关搜索的结果?
我需要根据windows phone 7中文本框中输入的文本突出显示搜索结果 通常的wpf代码在windows phone 7中不起作用。有人说如何在WindowsPhone7中实现这一点 实际上这是我用来填充联系人列表的xaml列表框C# 如何突出显示windows phone 7中上下文相关搜索的结果?,c#,windows-phone-7,xaml,C#,Windows Phone 7,Xaml,我需要根据windows phone 7中文本框中输入的文本突出显示搜索结果 通常的wpf代码在windows phone 7中不起作用。有人说如何在WindowsPhone7中实现这一点 实际上这是我用来填充联系人列表的xaml列表框 <ListBox Name="ContactList" ItemsSource="{Binding}" Margin="14,85,14,28" Foreground="White" SizeChanged="ContactList_SizeCh
<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#代码
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Windows;
//使用System.Windows.Controls;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Animation;
使用System.Windows.Shapes;
使用Microsoft.Phone.Controls;
使用Microsoft.Phone.UserData;
使用Newtonsoft.Json;
使用Newtonsoft.Json.Linq;
使用System.Collections.ObjectModel;
使用Microsoft.Phone.Shell;
使用System.Windows.Controls;
使用System.Windows.Media.Imaging;
使用System.Text.RegularExpressions;
命名空间SmartContactsApp
{
公共部分类主页:PhoneApplicationPage
{
私有列表lstAddress=新列表();
publicstringaddressjson=string.Empty;
//建造师
公共主页()
{
初始化组件();
this.Loaded+=新的RoutedEventHandler(主页面_-Loaded);
CreateSecondaryTile();
}
///
///要列出所有联系人。
///
私有无效联系人列表()
{
ContactList.DataContext=null;
Contacts cons=新联系人();
cons.SearchCompleted+=新事件处理程序(联系人\u SearchCompleted);
cons.SearchAsync(contactFilterString.Text,FilterKind.DisplayName,“Contacts”);
}
///
///要从移动联系人获取所有联系人。
///
///
///
公共无效联系人搜索已完成(对象发件人、联系人搜索目标e)
{
尝试
{
ContactList.DataContext=e.结果;
}
捕获(例外)
{
投掷;
}
}
private void contactFilterString_TextChanged(对象发送者,TextChangedEventArgs e)
{
联系人列表();
}
}
如何突出这一点,
提前感谢!对于类似的需求,我做了以下操作: 首先,我将以下内容作为ListBox数据模板的一部分
<StackPanel Width="450">
<TextBlock Text="{Binding text}" Width="450" TextWrapping="Wrap" FontSize="24" Visibility="Collapsed"/>
<RichTextBox Width="450" FontSize="24" Foreground="#FFFFFF"/>
</StackPanel>
其中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;
}
private T findfirstelementvisualtree(DependencyObject parentElement),其中T:DependencyObject
{
var count=VisualTreeHelper.GetChildrenCount(parentElement);
如果(计数=0)
返回null;
for(int i=0;i
因此,我使用一个隐藏的文本框,将实际文本的构造XAML代码绑定到该文本框。这是因为您无法直接绑定到RichTextBox或运行元素。然后在Size_changed处理程序中,我将XAML代码设置为可见的RichTextBox
我不确定它在多大程度上适合您的需要,您可能需要对上述流程进行许多更改,以使其适合您
祝你好运:)
更新:
用代码中的Address类替换我代码中的Result类
并将您的列表定义替换为“我的列表”
在Address类中添加一个名为“xamlCode”的附加属性。(将item.text2=item.text
行替换为item.xamlCode=item.DisplayName
剩下的你应该清楚了
如果有任何疑问的话,.我是在 Xaml代码:
<!--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\u TextChanged(对象发送者,TextChangedEventArgs e)
{
//联系人列表();
SearchVisualTree(联系人列表);
如果(contactFilterString.Text==“”)
{
联系人列表();
}
}
私有void SearchVisualTree(操作联系人列表)
{
SearchVisualTree(联系人列表);
}
私有void SearchVisualTree(DependencyObject targetElement)
{
变量计数=VisualTreeHelper.GetChildrenCount(targetElement);
for(int i=0;iprivate 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;
}
}
}
}
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;
}
<!--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>
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;
}
}