wpf 如何在TextBox(TextChanged=“OnTextChanged”)中的更多xaml文件中使用相同的方法

ewm0tg9j  于 2023-04-07  发布在  其他
关注(0)|答案(2)|浏览(132)

我有更多的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方法(无需复制/粘贴)?谢谢。

hjqgdpho

hjqgdpho1#

事件处理程序本身必须位于同一个类中,因此不能编写类似<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标记中将<TextBox TextChanged="OnTextChanged"/>替换为<local:CustomTextBox>

gojuced7

gojuced72#

有很多解决方案,其中之一是创建从TextBox派生的类
1.创建一个wpf NET项目(这里称为WpfApp3)

  1. add class(这里称为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)
        {

        }
    }
}

相关问题