简体   繁体   中英

WPF: Can i make a function (PreviewTextInput) into a Static Resource?

I have multiple textboxes in different controls that requires the same logic in their PreviewTextInput . Currently i have just copied my custom PreviewTextInput function into each control's code behind .

I would like to avoid copying code and just have the function in a singular place and bind all the different textboxes to that. So i figured i could use Static Resource , but i have no idea on how i would go on about doing that.

    The *PreviewTextInput* function i would like to access globally:
    public void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
    {
        Regex regex = new Regex("[^-0-9.,]+");
        e.Handled = regex.IsMatch(e.Text);
    }

I found one solution that i don't like since it requires to set the whole Template of the textbox. By adding the function above in Generic.xml 's code-behind, then in its xaml file:

<ControlTemplate x:Key="NumberValidationTextBox">
    <TextBox PreviewTextInput="NumberValidationTextBox"/>
</ControlTemplate>

then bind by setting Template="{StaticResource NumberValidationTextBox}". However, this will replace the Template. I only want to set the PreviewTextInput. Is it possible to create a Static Resource of this even function?

EDIT: Second solution i found, but i'm still not pleased with:

Define a regex with the rules somewhere that can be accessed globally (i did Generic.cs.xml):

public static Regex numberRegex = new Regex("[^-0-9.,]+");

Then inside the code-behind 's of each control that requires the regex for their textbox:

    public void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
    {
        e.Handled = Generic.numberRegex.IsMatch(e.Text);
    }

However, this still requires me to add a function in each control's code-behind. I would like to be able to stick to just XAML by binding the function from Generic.cs.xml (the function at the very top) into the controls directly with a static resource.

I would like to know if this is possible or if i should reluctantly settle with solution 2.

StaticResource ? No. But you could create an attached property that sets the regular expression:

public static class TextBoxExtensions
{
    public static readonly DependencyProperty RegularExpressionProperty =
        DependencyProperty.RegisterAttached("RegularExpression",
            typeof(string),
            typeof(TextBoxExtensions),
            new FrameworkPropertyMetadata(OnSet));

    public static string GetRegularExpression(TextBox target) =>
        (string)target.GetValue(RegularExpressionProperty);

    public static void SetRegularExpression(TextBox target, string value) =>
        target.SetValue(RegularExpressionProperty, value);

    private static void OnSet(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        TextBox textBox = (TextBox)d;
        textBox.PreviewTextInput += OnPreviewTextInput;
    }

    private static void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        string regularExpression = GetRegularExpression((TextBox)sender);
        if (!string.IsNullOrEmpty(regularExpression))
        {
            Regex regex = new Regex(regularExpression);
            e.Handled = regex.IsMatch(e.Text);
        }
    }
}

Usage:

<TextBox local:TextBoxExtensions.RegularExpression="[^-0-9.,]+"/>

This lets you specify a different regular expression for each TextBox if you want to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM