TextBox添加鼠标按下、失去焦点、鼠标移动等事件及重写

这篇具有很好参考价值的文章主要介绍了TextBox添加鼠标按下、失去焦点、鼠标移动等事件及重写。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

TextBox添加鼠标按下、失去焦点、鼠标移动等事件及重写

方法1:

//xaml
<TextBox Grid.Column="2" Grid.Row="0" x:Name="TBSeriesDescription" Text="{Binding CurrentSeries.ReconSeriesDescription,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"  VerticalAlignment="Stretch" FontSize="14" MaxLength="50">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="LostFocus">
            <i:InvokeCommandAction Command="{Binding TBSeriesDescriptionLostFocusCommand}" CommandParameter="{Binding ElementName=TBSeriesDescription}"/>
        </i:EventTrigger>
        <i:EventTrigger EventName="PreviewMouseDown">
            <i:InvokeCommandAction Command="{Binding TBSeriesDescriptionPreviewMouseDownCommand}" CommandParameter="{Binding ElementName=TBSeriesDescription}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>


//文本框焦点失去事件
public ICommand TBSeriesDescriptionLostFocusCommand { get; private set; }
//鼠标按下事件
public ICommand TBSeriesDescriptionPreviewMouseDownCommand { get; private set; }

//初始化
TBSeriesDescriptionLostFocusCommand = new DelegateCommand<object>(TBSeriesDescriptionLostFocusCommand_Execute);
TBSeriesDescriptionPreviewMouseDownCommand = new DelegateCommand<object>(TBSeriesDescriptionPreviewMouseDownCommand_Execute);

//文本框焦点失去事件触发的方法
private void TBSeriesDescriptionLostFocusCommand_Execute(object control)
{
    TextBox textBox = control as TextBox;
    textBox.SelectionLength = 0;
}
//鼠标按下事件
private void TBSeriesDescriptionPreviewMouseDownCommand_Execute(object control)
{
    TextBox tb = control as TextBox;
    if (tb == null || tb.IsFocused)
    {
        return;
    }
    tb.Focus();
}

方法2:

//TextBox类
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Common.Control
{
    /// <summary>
    /// 定制的TextBox
    /// </summary>
    public class CustomizedTextBox : TextBox
    {
        public CustomizedTextBox()
        {
            this.GotFocus += CustomizedTextBox_GotFocus;
            this.LostFocus += CustomizedTextBox_LostFocus;
            this.PreviewMouseDown += this.CustomizedTextBox_PreviewMouseDown;
            //this.GotMouseCapture += this.CustomizedTextBox_GotMouseCapture;
            //this.MouseLeave += this.CustomizedTextBox_MouseLeave;
            this.PreviewKeyDown += CustomizedTextBox_PreviewKeyDown;
        }

        /// <summary>
        /// 按下Backspace键,直接替换选择的文本框内容部分为空
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CustomizedTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {            
            TextBox tb = sender as TextBox;
            if (tb == null)
            {
                return;
            }
            if (e.Key == Key.Back && tb.SelectionLength > 0)
            {
                int start = tb.SelectionStart;
                int length = tb.SelectionLength;
                tb.Text = tb.Text.Remove(start, length);
                tb.SelectionStart = start;
            }
        }

        /// <summary>
        /// 鼠标离开事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CustomizedTextBox_MouseLeave(object sender, MouseEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb == null)
            {
                return;
            }
            Keyboard.ClearFocus();
        }

        /// <summary>
        /// 获取鼠标捕获事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CustomizedTextBox_GotMouseCapture(object sender, MouseEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb == null || tb.IsFocused)
            {
                return;
            }
            tb.Focus();
            //e.Handled = true;
        }

        /// <summary>
        /// 获得焦点时,文本框内容全选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CustomizedTextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb == null)
            {
                return;
            }
            tb.SelectAll();
            设置光标闪烁的位置
            //tb.CaretIndex = tb.Text.Length;
        }

        /// <summary>
        /// 文本框焦点失去事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CustomizedTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            TextBox textBox = sender as TextBox;
            textBox.SelectionLength = 0;
        }

        /// <summary>
        /// 鼠标按下时,如果未获得焦点,获得焦点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CustomizedTextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb == null || tb.IsFocused)
            {
                return;
            }
            tb.Focus();
            e.Handled = true;
        }

    }
}

//样式:
 <Style x:Key="CustomizedTextBoxStyle" TargetType="{x:Type TextBox}">
        <Setter Property="MaxLength" Value="50"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="BorderBrush" Value="{StaticResource SolidBrush_Gray10}" />
        <Setter Property="CaretBrush" Value="White"/>
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="MinHeight" Value="26" />
        <Setter Property="FontSize" Value="14"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Stretch"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Border SnapsToDevicePixels="True" CornerRadius="2" Background="{TemplateBinding Control.Background}" BorderBrush="{TemplateBinding Control.BorderBrush}" BorderThickness="{TemplateBinding Control.BorderThickness}">
                        <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True">
                            <Setter Value="{StaticResource SolidBrush_Gray3}" Property="BorderBrush" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Value="{StaticResource SolidBrush_Gray3}" Property="BorderBrush" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Value="0.5" Property="Opacity" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

//页面
<commonControl:CustomizedTextBox Grid.Column="2" Grid.Row="0" Text="{Binding CurrentSeries.ReconSeriesDescription,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" Style="{StaticResource CustomizedTextBoxStyle}"/>

 文章来源地址https://www.toymoban.com/news/detail-618386.html

 

到了这里,关于TextBox添加鼠标按下、失去焦点、鼠标移动等事件及重写的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包