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

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

方法1:

//xaml

    
        
            
        
        
            
        
    



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

//初始化
TBSeriesDescriptionLostFocusCommand = new DelegateCommand(TBSeriesDescriptionLostFocusCommand_Execute);
TBSeriesDescriptionPreviewMouseDownCommand = new DelegateCommand(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
{
    /// 
    /// 定制的TextBox
    /// 
    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;
        }

        /// 
        /// 按下Backspace键,直接替换选择的文本框内容部分为空
        /// 
        /// 
        /// 
        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;
            }
        }

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

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

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

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

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

    }
}

//样式:
 

//页面

 

 

你可能感兴趣的:(C#,WPF,c#,wpf)