如何在文本框中的更多 xaml 文件中使用相同的方法 (文本更改= "OnTextChanged" )



我有更多的xaml文件。在每个文件中我有:

<TextBox TextChanged="OnTextChanged"/>

后面的代码是:

private void OnTextChanged(object sender, TextChangedEventArgs e)
{
var myInput = sender as TextBox;
myInput.Text = myInput.Text.Replace(",", ".").Trim();
myInput.CaretIndex = myInput.Text.Length;
}

如何在其他xaml文件中使用OnTexChanged方法(无需复制/粘贴)?谢谢。

事件处理程序本身必须位于同一个类中,因此您不能编写类似<TextBox TextChanged="MyOtherClassName.OnTextChanged"/>的内容。

但是要封装实际的功能,您可以创建一个自定义(但非常简单)的TextBox类:

public class CustomTextBox : TextBox
{
protected override void OnTextChanged(TextChangedEventArgs e)
{
base.OnTextChanged(e);
Text = Text.Replace(",", ".").Trim();
CaretIndex = Text.Length;
}
}

要重用该功能,您只需使用您的自定义控件而不是内置的TextBox控件,即您在XAML标记中用<local:CustomTextBox>替换<TextBox TextChanged="OnTextChanged"/>

有很多解决方案,一个是从TextBox

派生的类的创建
  1. 创建wpf . NET项目(这里称为WpfApp3)

  2. 添加类(这里称为ValidatedTextBox)

ValidatedTextBox.cs

using System.Text.RegularExpressions;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows;
namespace WpfApp3
{
public class ValidatedTextBox : TextBox
{
protected override void OnInitialized(System.EventArgs e)
{
base.OnInitialized(e);
//default values, just for test
HorizontalContentAlignment = HorizontalAlignment.Left;
VerticalContentAlignment = VerticalAlignment.Center;
Margin = new Thickness(5, 5, 5, 5);
Padding = new Thickness(5, 5, 5, 5);
}
protected override void OnPreviewTextInput(TextCompositionEventArgs e)
{           
base.OnPreviewTextInput(e);
Regex regex = new Regex("^-?[0-9]*[\.,]?[0-9]?[0-9]?$");
var futureText = $"{Text}{e.Text}";
e.Handled = !regex.IsMatch(futureText);
}
protected override void OnTextChanged(TextChangedEventArgs e)
{
base.OnTextChanged(e);
Text = Text.Replace(",", ".").Trim();
CaretIndex = Text.Length;
}
}
}

MainWindow.xaml

<Window x:Class="WpfApp3.MainWindow" x:Name="Root"
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"
xmlns:local="clr-namespace:WpfApp3"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<local:ValidatedTextBox Grid.Column="0"  Background="AliceBlue" Width="100" Height="50" Text="{Binding Val1, Mode=TwoWay}"/>
<local:ValidatedTextBox Grid.Column="1"  Background="AliceBlue" Width="100" Height="50" Text="{Binding Val2, Mode=TwoWay}"/>
<Button Grid.Column="3" Content="GetData" Height="59" Click="Button_Click"/>
</Grid>
</Window>

Mainwindow.xaml.cs:

using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
namespace WpfApp3
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
private string val1;
public string Val1
{
get { return val1; }
set 
{ 
val1 = value;
OnPropertyChanged("Val1");
}
}
private string val2;
public string Val2
{
get { return val2; }
set
{
val2 = value;
OnPropertyChanged("Val2");
}
}
public MainWindow()
{
Val1 = "2";
InitializeComponent();
DataContext = this;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Debug.WriteLine($"value: {Val1}");
System.Diagnostics.Debug.WriteLine($"value: {Val2}");
}
void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private void ValidatedTextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
}
}
}

我找到了最适合我的解决方案:

ResourceDictionary.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MyName.ResourceDictionary"
x:ClassModifier="public">
<Style TargetType="TextBox">
<EventSetter Event="PreviewTextInput" Handler="NumberValidationTextBox"/>
<EventSetter Event="TextChanged" Handler="OnTextChanged"/>
</Style>
</ResourceDictionary>

背后的代码:

public void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("^-?[0-9]*[\.,]?[0-9]?[0-9]?$");
var futureText = $"{(sender as TextBox).Text}{e.Text}";
e.Handled = !regex.IsMatch(futureText);
}
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
var myInput = sender as TextBox;
myInput.Text = myInput.Text.Replace(",", ".").Trim();
myInput.CaretIndex = myInput.Text.Length;
}

在所有想使用TextBox的.xaml文件中:

<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="StyleTemplate.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>

最新更新