如何写检查,如果一个TextBlock包含一个特定的字符串在windows phone 7 XAML

本文关键字:字符串 XAML phone windows 包含一 TextBlock 检查 何写 如果 一个 | 更新日期: 2023-09-27 17:49:20

我有一个文本块和一个文本框,我需要检查文本块是否包含文本框的值,如果是,那么这个特定的值应该用一些颜色突出显示…

例如,

TextBlock。Text="Just a Test"

如果我们在TextBox中输入" the ",那么文本块中的值应该高亮显示为"Just a * te *st"

在xaml。

如果有人知道意思请说!

提前感谢!

如何写检查,如果一个TextBlock包含一个特定的字符串在windows phone 7 XAML

嗨,我找到了解决方案,这里是在MVVM模式下设计的底层代码:编辑:** Pages -> mainpage . example .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>

现在mainpage . example .cs

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;
}

现在的数据绑定类(MOdel类)

public class CheckList
{
    public string DisplayName { get; set; }
}

谢谢:)如果你想问或者有任何疑问,你可以问更多的问题。

这里有一个示例代码放入文本框TextChange event

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";
        }
    }

这将突出显示文本,如果没有找到匹配则返回no match。

注意:此代码段区分大小写