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文章来源:https://www.toymoban.com/news/detail-618386.html
到了这里,关于TextBox添加鼠标按下、失去焦点、鼠标移动等事件及重写的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!