C# 如何在WindowsPhone7XAML中编写文本块是否包含特定字符串的检查?
我有一个textblock和一个textbox,我需要检查textblock是否包含textbox值,如果是,则该特定值应以某种颜色突出显示 比如说, Text block.Text=“只是一个测试” 如果在文本框中键入“te”,则文本块中的值应高亮显示为“Just a*te*st” 在xaml中 如果有人知道你的意思,请说C# 如何在WindowsPhone7XAML中编写文本块是否包含特定字符串的检查?,c#,windows-phone-7,xaml,C#,Windows Phone 7,Xaml,我有一个textblock和一个textbox,我需要检查textblock是否包含textbox值,如果是,则该特定值应以某种颜色突出显示 比如说, Text block.Text=“只是一个测试” 如果在文本框中键入“te”,则文本块中的值应高亮显示为“Just a*te*st” 在xaml中 如果有人知道你的意思,请说 提前谢谢 以下是要放入文本框的示例代码TextChange事件 private void textBox1_TextChanged(object sender, TextC
提前谢谢 以下是要放入文本框的示例代码
TextChange
事件
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
string text = "this is a TextBlock";
textBlock1.Text = text;
textBlock1.Inlines.Clear();
int index = text.IndexOf(textBox1.Text);
int lenth = textBox1.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.White);
}
else
{
textBlock1.Text = "No Match";
}
}
private void textBox1\u TextChanged(对象发送者,textchangedventargs e)
{
string text=“这是一个文本块”;
textBlock1.Text=Text;
textBlock1.Inlines.Clear();
int index=text.IndexOf(textBox1.text);
int lenth=textBox1.Text.Length;
如果(!(索引<0))
{
Run Run=new Run(){Text=Text.Substring(index,lenth),fontwweight=fontwweights.ExtraBold};
run.Foreground=新的SolidColorBrush(Colors.Orange);
textBlock1.Inlines.Add(new Run(){Text=Text.Substring(0,索引),fontwweight=fontwweights.Normal});
textBlock1.Inlines.Add(运行);
textBlock1.Inlines.Add(new Run(){Text=Text.Substring(index+lenth),fontwweight=fontwweights.Normal});
textBlock1.FontSize=30;
textBlock1.Foreground=新的SolidColorBrush(Colors.White);
}
其他的
{
textBlock1.Text=“不匹配”;
}
}
这将突出显示文本,如果未找到匹配项,则返回no match
注意:此代码段区分大小写。您好,我找到了解决方案。以下是以MVVM模式设计的底层代码: 编辑:**Pages->MainPage.xaml.cs和ListViewModal已根据您的需要进行了更改 **视图模式中有一些问题绑定数据,所以在代码隐藏中快速响应 MainPage.xaml
<phone:PhoneApplicationPage
x:Class="FirstAppInCSharp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Width="Auto" Height="Auto"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True" d:DesignHeight="768" d:DesignWidth="480">
<Grid Name='LayoutRoot'>
<ListBox Height='500' Width='500' Background='Red'
Name="ContactList"
Margin="14,85,14,28" Loaded='ContactList_Loaded'
Foreground="Black"
ItemsSource='{Binding ListOftext}'
>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel
Background='AliceBlue' Height='100' Width='500'
Orientation="Horizontal">
<TextBlock
FontSize='30'
Height='70'
Foreground='Black'
Text='{Binding DisplayName}' Width='300'
/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBox
Height='72'
HorizontalAlignment='Left'
Margin='8,27,0,0'
Name='textBox1'
TextChanged='textBox1_TextChanged'
VerticalAlignment='Top'
Width='460' />
</Grid>
谢谢:)如果你想或者有任何疑问,你可以问更多的问题 这是因为我正在使用Microsoft.Phone.Userdata.contacts。我的问题是在这里,你有没有看到我的问题的链接。我们不能在数据模板内使用textblock。这就是我要的xaml。目前我没有一个系统来检查,但如果可能的话,我肯定会在明天回答你:)我错过了链接,我想你后来添加了它。。。或者我可能错过了:)这将适用于用户定义的列表,但我不明白,如何在列表视图中填充手机的联系人列表…感谢您的回复!对不起打扰你了。你能谈谈SearchVisualTree方法吗。您的代码运行良好,但列表只填充一次。列表不会根据文本更改而更改。GetChild(targetElement,i);这是搜索子元素的代码的主要部分。如果你调试你的应用程序,你可以识别事物是如何迭代可视化树的,它实际上是树结构中元素作为子元素和父元素的对齐方式。我们从父节点开始搜索。。这是联系人列表,检查每个分支元素。如果找不到元素,那么我们将移动到下一个分支。希望这个解释有帮助。不包括技术细节,您更容易理解:)
public partial class MainPage : PhoneApplicationPage
{
TextBlock textBlock1 = null;
List<TextBlock> listText = null;
// Constructor
public MainPage()
{
InitializeComponent();
Contacts contact = new Contacts();
contact.SearchAsync("", FilterKind.DisplayName, null);
contact.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(contact_SearchCompleted);
}
void contact_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
ContactList.DataContext = new ListViewModal(e.Results);
}
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
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
{
SearchVisualTree(child);
}
}
}
private void HighlightText()
{
if (textBlock1 != null)
{
string text = textBlock1.Text;
textBlock1.Text = text;
textBlock1.Inlines.Clear();
int index = text.IndexOf(textBox1.Text);
int lenth = textBox1.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";
}
}
}
private void ContactList_Loaded(object sender, RoutedEventArgs e)
{
}
}
public class ListViewModal : INotifyPropertyChanged
{
public List<CheckList> ListOftext { get; set; }
public ListViewModal(IEnumerable<Contact> iEnumerable)
{
ListOftext = new List<CheckList>();
foreach (var list in iEnumerable)
{
ListOftext.Add(new CheckList(){DisplayName = list.DisplayName});
}
RaisePropertyChanged("ListOftext");
}
/// <summary>
/// Property changed method
/// Executes when a property changes its value
/// </summary>
/// <param name="propertyName"></param>
public void RaisePropertyChanged(string propertyName)
{
// this is the property changed method
//to detect property change
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
private IEnumerable<Contact> iEnumerable;
}
public class CheckList
{
public string DisplayName { get; set; }
}