创建wp7自定义搜索控件SearchBox

本文介绍了一种基于TextBox自定义的搜索控件,新增了六种依赖属性,包括Hint、MinCharacters、Text、ErrorMessage、HighLightBorderBrush和Command。文章详细展示了控件的功能特性、依赖属性设置及样式模板。

该自定义搜索控件继承TextBox,并且又添加了6种自定义依赖属性,分别为

Hint:暗示输入类型

MinCharacts:检验最少输入字符,如果小于该值则不能执行搜索命令

Text:该值重写了TextBox的Text属性,并且可以及时更新Text值,而不是等到失去焦点的时候才去更新

ErrorMessage:错误信息,当检验不符合条件时候在右下方显示error信息

HighLightBorderBrush:当有错误信息时候,给SearchBox添加指定颜色的边框

Command:按回车键时候,当验证通过时候执行搜索命令



效果图

    

SearchBox.cs 依赖属性声明:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace CommonUI.CustomControls
{
    /// <summary>
    /// A custom search control
    /// </summary>
    public class SearchBox : TextBox
    {
        private ContentControl HintContent;
        private TextBlock ErrorContent;

        public static readonly DependencyProperty HintProperty =
            DependencyProperty.Register("Hint", typeof(string), typeof(SearchBox), new PropertyMetadata(OnHintPropertyChanged));

        public static readonly DependencyProperty MinCharactersPerperty =
            DependencyProperty.Register("MinCharacters", typeof(int), typeof(SearchBox), new PropertyMetadata(0));

        public static readonly new DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(SearchBox), new PropertyMetadata(default(string), OnTextChanged));

        public static readonly new DependencyProperty ErrorMessageProperty =
           DependencyProperty.Register("ErrorMessage", typeof(string), typeof(SearchBox), new PropertyMetadata(null));

        public static readonly new DependencyProperty HighLightBorderBrushProperty =
           DependencyProperty.Register("HighLightBorderBrush", typeof(Brush), typeof(SearchBox), new PropertyMetadata(null));

        public static readonly new DependencyProperty CommandProperty =
           DependencyProperty.Register("Command", typeof(ICommand), typeof(SearchBox), null);

        #region Properties fields

        /// <summary>
        /// Get and set methods for the Text dependency property value.
        /// so that the value can update immediately
        /// </summary>
        public new string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        /// <summary>
        /// Get and set methods for the ErrorMessage dependency Property value.
        /// </summary>
        public string ErrorMessage
        {
            get { return (string)GetValue(ErrorMessageProperty); }
            set { SetValue(ErrorMessageProperty, value); }
        }

        /// <summary>
        /// Get and set methods for the HighLightBorderBrush dependency property value.
        /// </summary>
        public Brush HighLightBorderBrush
        {
            get { return (Brush)GetValue(HighLightBorderBrushProperty); }
            set { SetValue(HighLightBorderBrushProperty, value); }
        }

        /// <summary>
        /// Get and set methods for the Hint dependency property value.
        /// </summary>
        public string Hint
        {
            get { return (string)GetValue(HintProperty); }
            set { this.SetValue(HintProperty, value); }
        }

        /// <summary>
        /// Get and set methods for the MinCharacters dependency property value.
        /// the default value is 0, if the value is bigger than 0, the control will check the length and show the highlight border
        /// </summary>
        public int MinCharacters
        {
            get { return (int)this.GetValue(MinCharactersPerperty); }
            set { this.SetValue(MinCharactersPerperty, value); }
        }

        /// <summary>
        /// Get and set methods for the Command dependency property value.
        /// Command triggered when the KeyUp with the ENTER key occurs.
        /// </summary>
        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        #endregion

        public SearchBox()
        {
            DefaultStyleKey = this.GetType();
            base.TextChanged += (s, e) =>
            {
                Text = base.Text;
            };
        }

        #region Override methods

        protected override void OnKeyUp(KeyEventArgs e)
        {
         if (!string.IsNullOrEmpty(this.Text))
         {
            if (e.Key == Key.Enter && this.Text.Length >= this.MinCharacters)
            {
                this.Command.Execute(this.Text);
            }
            if (e.Key == Key.Unknown && this.Text.Length >= this.MinCharacters)
            {
                if (e.PlatformKeyCode == 10)
                {
                    this.Command.Execute(this.Text);
                }
            }
          }
	}
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            this.HintContent = this.GetTemplateChild("hintContent") as ContentControl;
            this.ErrorContent = this.GetTemplateChild("errorContent") as TextBlock;
            if (ErrorContent != null)
            {
                HideErrorContent(true);
            }
            if (HintContent != null)
            {
                DetermineHintContentVisibility();
            }
        }

        protected override void OnGotFocus(RoutedEventArgs e)
        {
            if (HintContent != null && string.IsNullOrEmpty(this.Text))
            {
                this.HintContent.Visibility = Visibility.Collapsed;
            }
            if (((SolidColorBrush)this.BorderBrush).Color == Color.FromArgb(191, 255, 255, 255))
            {
                this.BorderBrush = new SolidColorBrush(Colors.White);
            }

            base.OnGotFocus(e);
        }

        protected override void OnLostFocus(RoutedEventArgs e)
        {
            if (HintContent != null && string.IsNullOrEmpty(this.Text))
            {
                this.HintContent.Visibility = Visibility.Visible;
                this.ClearValue(BorderBrushProperty);
                HideErrorContent(true);
            }
            if (((SolidColorBrush)this.BorderBrush).Color == Colors.White)
            {
                this.ClearValue(BorderBrushProperty);
            }

            base.OnLostFocus(e);
        }

        #endregion

        #region Private methods

        private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            SearchBox txt = d as SearchBox;
            txt.Text = (string)e.NewValue;
            if (txt.Text.Length < (int)d.GetValue(MinCharactersPerperty))
            {
                txt.BorderBrush = txt.GetValue(HighLightBorderBrushProperty) as Brush;
                txt.HideErrorContent(false);
            }
            else
            {
                if ((int)d.GetValue(MinCharactersPerperty) != 0)
                {
                    txt.HideErrorContent(true);
                    txt.BorderBrush = new SolidColorBrush(Colors.White);
                }
            }
        }

        private static void OnHintPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            SearchBox textBox = sender as SearchBox;
            if (textBox != null && textBox.HintContent != null)
            {
                textBox.DetermineHintContentVisibility();
            }
        }

        private void HideErrorContent(bool hide)
        {
            this.ErrorContent.Visibility = hide ? Visibility.Collapsed : Visibility.Visible;
        }

        private void DetermineHintContentVisibility()
        {
            if (string.IsNullOrEmpty(this.Text))
            {
                this.HintContent.Visibility = Visibility.Visible;
            }
            else
            {
                this.HintContent.Visibility = Visibility.Collapsed;
            }
        }

        #endregion
    }
}


Style:SearchBox.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:local="clr-namespace:CommonUI.CustomControls">

    <Style TargetType="local:SearchBox">
        <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilyNormal}"/>
        <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/>
        <Setter Property="Background" Value="{StaticResource PhoneTextBoxBrush}"/>
        <Setter Property="Foreground" Value="{StaticResource PhoneTextBoxForegroundBrush}"/>
        <Setter Property="BorderBrush" Value="{StaticResource PhoneTextBoxBrush}"/>
        <Setter Property="SelectionBackground" Value="{StaticResource PhoneAccentBrush}"/>
        <Setter Property="SelectionForeground" Value="{StaticResource PhoneTextBoxSelectionForegroundBrush}"/>
        <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
        <Setter Property="Padding" Value="2"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:SearchBox">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="EnabledBorder">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Collapsed</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="DisabledOrReadonlyBorder">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="ReadOnly">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="EnabledBorder">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Collapsed</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="DisabledOrReadonlyBorder">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="DisabledOrReadonlyBorder">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="DisabledOrReadonlyBorder">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="DisabledOrReadonlyContent">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxReadOnlyBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="FocusStates">
                                <VisualState x:Name="Focused">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="EnabledBorder">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxEditBackgroundBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Unfocused"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Grid Background="Transparent" Grid.Row="0" Margin="{TemplateBinding Margin}">
                            <Border x:Name="EnabledBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Margin="{StaticResource PhoneTouchTargetOverhang}">
                                <Grid>
                                    <ContentControl x:Name="hintContent" Content="{TemplateBinding Hint}" HorizontalAlignment="Left" Background="Transparent" Opacity="0.5"/>
                                    <ContentControl x:Name="ContentElement" BorderThickness="0" HorizontalContentAlignment="Stretch" Margin="{StaticResource PhoneTextBoxInnerMargin}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="Stretch"/>
                                </Grid>
                            </Border>
                            <Border x:Name="DisabledOrReadonlyBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="Transparent" Margin="{StaticResource PhoneTouchTargetOverhang}" Visibility="Collapsed">
                                <TextBox x:Name="DisabledOrReadonlyContent" Background="Transparent" Foreground="{StaticResource PhoneDisabledBrush}"
                                         FontWeight="{TemplateBinding FontWeight}" FontStyle="{TemplateBinding FontStyle}" FontSize="{TemplateBinding FontSize}"
                                         FontFamily="{TemplateBinding FontFamily}" IsReadOnly="True" SelectionForeground="{TemplateBinding SelectionForeground}"
                                         SelectionBackground="{TemplateBinding SelectionBackground}" TextAlignment="{TemplateBinding TextAlignment}"
                                         Height="{TemplateBinding Height}" Width="{TemplateBinding Width}"
                                         HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}"
                                         TextWrapping="{TemplateBinding TextWrapping}" Text="{TemplateBinding Text}"/>
                            </Border>
                        </Grid>
                        <TextBlock x:Name="errorContent" Grid.Row="1" Text="{TemplateBinding ErrorMessage}" Style="{StaticResource ErrorContentStyle}" Foreground="{TemplateBinding HighLightBorderBrush}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="TextBlock" x:Key="ErrorContentStyle">
        <Setter Property="Margin" Value="0,-10,10,0"/>
        <Setter Property="FontSize" Value="20"/>
        <Setter Property="HorizontalAlignment" Value="Right"/>
    </Style>
</ResourceDictionary>

页面

<local:SearchBox Hint="{Binding User.CustomerSearchHint, Source={StaticResource LocalizationManager}}"                             x:Name="searchBorder" Padding="5" MinCharacters="3" HighLightBorderBrush="Red" BorderThickness="3"                             HorizontalAlignment="Stretch"  Text="{Binding SearchString, Mode=TwoWay}" ErrorMessage="{Binding User.ErrorSearchString, Source={StaticResource LocalizationManager}}" Command="{Binding SearchCommand}"/>


源码链接: https://pan.quark.cn/s/fa13cd6c6c8d Chrome浏览器作为一款备受青睐的网页浏览器,凭借其出色的稳定性和运行速度获得了广泛认可。 然而出于安全考量,Chrome系统默认不兼容ActiveX插件,因为ActiveX技术主要应用于Internet Explorer,它赋予网页内容与用户本地系统交互的能力,但同时也可能引发潜在的安全隐患。 不过在某些特定工作场景下,比如在企业内部网络环境或需要与老旧应用程序整合时,可能仍需在Chrome中启用ActiveX控件。 为此我们必须掌握在Chrome浏览器下加载和运用ActiveX的方法。 首先需要明确ActiveX的本质。 ActiveX是由微软设计的一种技术框架,旨在开发可在网页环境中运行的控件,这些控件能够完成多种功能,包括视频播放、应用程序组件运行或与硬件设备通信等。 ActiveX控件多以OCX(OLE控件)格式发布。 在Chrome浏览器中启用ActiveX需要采取额外措施,因为该浏览器本身并不支持此项技术。 以下是几种常见的解决方案: 1. **应用Chrome的兼容性设置**:部分Chrome版本提供了" --enable-internal-activex"命令行参数,可通过此参数使浏览器具备加载ActiveX控件的能力。 用户可在启动Chrome时,于快捷方式的目标路径后附加该参数来激活此功能。 例如:"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --enable-internal-activex。 2. **安装第三方插件**:市面上存在一些第三方插件,例如"IE Tab"或"ActiveX Con...
标题SpringBoot与微信小程序结合的健康饮食平台研究AI更换标题第1章引言介绍健康饮食平台的研究背景、意义、国内外研究现状、论文方法及创新点。1.1研究背景与意义阐述健康饮食平台在当前社会的重要性及其市场需求。1.2国内外研究现状分析国内外健康饮食平台的发展现状及趋势。1.3研究方法及创新点概述本文采用的研究方法和技术创新点。第2章相关理论总结健康饮食、SpringBoot及微信小程序的相关理论。2.1健康饮食理论介绍健康饮食的基本原则和营养学知识。2.2SpringBoot框架阐述SpringBoot框架的特点、优势及在项目中的应用。2.3微信小程序技术介绍微信小程序的开发技术、特点及其用户群体。第3章健康饮食平台设计详细介绍健康饮食平台的设计方案,包括前端和后端设计。3.1平台架构设计给出平台的整体架构、模块划分及交互流程。3.2数据库设计介绍数据库的设计思路、表结构及数据关系。3.3前后端交互设计阐述前后端数据交互的方式、接口设计及安全性考虑。第4章微信小程序实现介绍微信小程序的具体实现过程,包括页面设计、功能实现等。4.1页面设计与布局给出微信小程序的页面设计思路、布局及交互效果。4.2功能实现与测试详细介绍微信小程序各项功能的实现过程及测试方法。4.3用户体验优化阐述如何提升微信小程序的用户体验,包括界面优化、性能优化等。第5章平台测试与优化对健康饮食平台进行测试,并根据测试结果进行优化。5.1测试环境与数据介绍测试环境、测试数据及测试方法。5.2测试结果分析从功能、性能、用户体验等方面对测试结果进行详细分析。5.3平台优化策略根据测试结果提出平台优化策略,包括代码优化、功能改进等。第6章结论与展望总结本文的研究成果,并展望未来的研究方向。6.1研究结论概括本文的主要研究结论和平台实现效果。6.2展望指出本文研究的不足之处以及未来研究的方向和改进点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值