• 自定义 匹配文本框


    <UserControl x:Class="PhoneApp4.CtrlView.MatchTextView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" x:Name="this"
        FontFamily="{StaticResource PhoneFontFamilyNormal}"
        FontSize="{StaticResource PhoneFontSizeNormal}"
        Foreground="{StaticResource PhoneForegroundBrush}"
        d:DesignHeight="480" d:DesignWidth="480">
    
        <Grid x:Name="LayoutRoot" Background="Transparent">
            <RichTextBox FontFamily="{Binding FontFamily, ElementName=this}" FontSize="{Binding FontSize, ElementName=this}"
                         Foreground="{Binding Foreground, ElementName=this}" FontStyle="{Binding FontStyle, ElementName=this}">
                <Paragraph x:Name="paragraph">
                    
                </Paragraph>
            </RichTextBox>
        </Grid>
    </UserControl>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using System.Windows.Documents;
    using System.Windows.Media;
    
    namespace PhoneApp4.CtrlView
    {
        public partial class MatchTextView : UserControl
        {
            public MatchTextView()
            {
                InitializeComponent();
            }
    
            public static readonly DependencyProperty TextProperty =
                                   DependencyProperty.Register("Text", typeof(string), typeof(MatchTextView),
                                   new PropertyMetadata(string.Empty, OnTextChanged));
            public string Text
            {
                get { return (string)(base.GetValue(TextProperty)); }
                set { base.SetValue(TextProperty, value); }
            }
            private static void OnTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
            {
                MatchTextView ctrl = sender as MatchTextView;
                ctrl.OnBufChanged((string)(args.NewValue));
            }
            private void OnBufChanged(string buf)
            {
                OnUpdataUi(buf, MatchText);
            }
    
            public static readonly DependencyProperty MatchTextProperty =
                                   DependencyProperty.Register("MatchText", typeof(string), typeof(MatchTextView),
                                   new PropertyMetadata(string.Empty, OnMatchTextChanged));
            public string MatchText
            {
                get { return (string)(base.GetValue(MatchTextProperty)); }
                set { base.SetValue(MatchTextProperty, value); }
            }
            private static void OnMatchTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
            {
                MatchTextView ctrl = sender as MatchTextView;
                ctrl.OnMatchBufChanged((string)(args.NewValue));
            }
            private void OnMatchBufChanged(string matchBuf)
            {
                OnUpdataUi(Text, matchBuf);
            }
    
            private void OnUpdataUi(string text, string matchText)
            {
                List<KeyValuePair<bool, string>> lst = new List<KeyValuePair<bool, string>>();
                if (!string.IsNullOrEmpty(text))
                {
                    if (!string.IsNullOrEmpty(matchText) && text.Contains(matchText))
                    {
                        do
                        {
                            int index = text.IndexOf(matchText);
                            if (index == 0)
                            {
                                lst.Add(new KeyValuePair<bool, string>(true, matchText));
                                text = text.Substring(matchText.Length);
                            }
                            else if (index > 0)
                            {
                                string buf = text.Substring(0, index);
                                lst.Add(new KeyValuePair<bool, string>(false, buf));
                                lst.Add(new KeyValuePair<bool, string>(true, matchText));
                                text = text.Substring(index + matchText.Length);
                            }
                        } while (text.Contains(matchText));
                        if (!string.IsNullOrEmpty(text))
                        {
                            lst.Add(new KeyValuePair<bool, string>(false, text));
                        }
                    }
                    else
                    {
                        lst.Add(new KeyValuePair<bool, string>(false, text));
                    }
                }
    
                Add(lst);
            }
            private void Add(List<KeyValuePair<bool, string>> lst)
            {
                this.paragraph.Inlines.Clear();
                for (int i = 0; i < lst.Count; i++)
                {
                    Run run = new Run();
                    run.Text = lst[i].Value;
                    if (lst[i].Key)
                    {
                        run.Foreground = MatchForeground;
                        run.FontSize = MatchFontSize;
                        run.FontStyle = MatchFontStyle;
                    }
                    this.paragraph.Inlines.Add(run);
                }
            }
    
            public static readonly DependencyProperty MatchForegroundProperty =
                                   DependencyProperty.Register("MatchForeground", typeof(Brush), typeof(MatchTextView),
                                   new PropertyMetadata(new SolidColorBrush(Colors.Yellow), OnMatchForegroundChanged));
            public Brush MatchForeground
            {
                get { return (Brush)(base.GetValue(MatchForegroundProperty)); }
                set { base.SetValue(MatchForegroundProperty, value); }
            }
            private static void OnMatchForegroundChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
            {
                MatchTextView ctrl = sender as MatchTextView;
                Brush brush = args.NewValue as Brush;
                if (brush != null)
                {
                    ctrl.foregroundChanged(brush);
                }
            }
            private void foregroundChanged(Brush foreground)
            {
                OnStyleChanged(foreground, MatchFontSize, MatchFontStyle);
            }
    
            public static readonly DependencyProperty MatchFontSizeProperty =
                                   DependencyProperty.Register("MatchFontSize", typeof(double), typeof(MatchTextView),
                                   new PropertyMetadata((double)(App.Current.Resources["PhoneFontSizeNormal"]), OnFontSizeChanged));
            public double MatchFontSize
            {
                get { return (double)(base.GetValue(MatchFontSizeProperty)); }
                set { base.SetValue(MatchFontSizeProperty, value); }
            }
            private static void OnFontSizeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
            {
                MatchTextView ctrl = sender as MatchTextView;
                ctrl.fontSizeChanged((double)(args.NewValue));
            }
            private void fontSizeChanged(double fontsize)
            {
                OnStyleChanged(MatchForeground, fontsize, MatchFontStyle);
            }
    
            public static readonly DependencyProperty MatchFontStyleProperty =
                                   DependencyProperty.Register("MatchFontStyle", typeof(FontStyle), typeof(MatchTextView),
                                   new PropertyMetadata(FontStyles.Normal, OnFontStyleChanged));
            public FontStyle MatchFontStyle
            {
                get { return (FontStyle)(base.GetValue(MatchFontStyleProperty)); }
                set { base.SetValue(MatchFontStyleProperty, value); }
            }
            private static void OnFontStyleChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
            {
                MatchTextView ctrl = sender as MatchTextView;
                ctrl.fontStyleChanged((FontStyle)(args.NewValue));
            }
            private void fontStyleChanged(FontStyle fontstyle)
            {
                OnStyleChanged(MatchForeground, MatchFontSize, fontstyle);
            }
    
            private void OnStyleChanged(Brush brush, double fontsize, FontStyle fontstyle)
            {
                for (int i = 0; i < paragraph.Inlines.Count; i++)
                {
                    if (!string.IsNullOrEmpty((paragraph.Inlines[i] as Run).Text) && (paragraph.Inlines[i] as Run).Text.Equals(MatchText))
                    {
                        (paragraph.Inlines[i] as Run).Foreground = brush;
                        (paragraph.Inlines[i] as Run).FontSize = fontsize;
                        (paragraph.Inlines[i] as Run).FontStyle = fontstyle;
                    }
                }
            }
    
        }
    }
    
    //public double FontSize { get; set; }
    
    //public FontStretch FontStretch { get; set; }
    
    //public FontStyle FontStyle { get; set; }
    //       Italic    Normal
    
    //public FontWeight FontWeight { get; set; }
    
    //public Brush Foreground { get; set; }

    使用案例:

    <CtrlView:MatchTextView Grid.Row="1" Margin="30,20,30,0" Text="19781031" FontSize="30" MatchFontSize="40" MatchText="1" MatchForeground="YellowGreen"/>

  • 相关阅读:
    在线程中更新UI
    Panel容器
    ImageList组件
    PrograssBar控件
    PictureBox控件
    GroupBox控件
    Timer控件Forms.Timer\System.Timers.Timer\System.Threading.Timer
    DataGridView控件1——手动添加数据,遍历数据
    SplitContainer控件
    Git理论知识
  • 原文地址:https://www.cnblogs.com/qq278360339/p/3786360.html
Copyright © 2020-2023  润新知