6

So I followed the guide on the following site to restrict the characters a textbox can accept.

http://www.rhyous.com/2010/06/18/how-to-limit-or-prevent-characters-in-a-textbox-in-csharp/

My problem is I can't figure out how to make the event handler trigger in the secondary class. Basically how do I tell VS to look for the event handler code in that class instead of MainWindow? I tried searching, but apparently don't know the correct terms to use. The xaml reference I used was

xmlns:DigitBox="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"

Any ideas?

H.B.
  • 166,899
  • 29
  • 327
  • 400
Keven M
  • 972
  • 17
  • 47

2 Answers2

3

Simplest way I've found to do it is assign the event in your constructor.

public MainWindow()
    {
        InitializeComponent();
        TextBoxCurrency.GotFocus += expandedTextBoxEvents.TextBoxCurrencyGotFocus;
        TextBoxCurrency.LostFocus += expandedTextBoxEvents.TextBoxCurrencyLostFocus;
    }

I've searched a way to do it in XAML and I did not found an easy and clean way to do it.

Guish
  • 4,968
  • 1
  • 37
  • 39
  • To complete the answer, the window object can be passed to the constructor to access to the components. `new TextBoxCurrency(this);` – A. Morel Nov 09 '19 at 07:03
1

You are much better off using commands and command bindings. I'm not sure what the specific command that would would bind to for a text box for your desired functionality, but one of the goals for WPF was to lessen the use of Event Handlers in code behind.

Check out this article for an overview of commands and this article for a way to hook up commands with events. WPF commanding is one of the coolest features to enable true separation of concerns between UI and business logic.

As a worst case scenario solution, you could create your own text box that inherits from the text box control and hook up the events in that class. Your control would then be reusable.

Nate Noonen
  • 1,371
  • 9
  • 19
  • The text box does inherit from the regular text box control...I just don't know how to let wpf that :). It keeps trying to make it use the normal textbox – Keven M Nov 05 '11 at 18:03
  • Are you adding it like a normal text box? You should be adding it explicity: In your window definition add something like this: xmlns:inh="clr-namespace:MyNamespace.InheritedControls" and then when you add the text box add it like this: – Nate Noonen Nov 05 '11 at 19:11
  • Ok, I will try that when I get home. In that text box xaml line do I modify the standard event call at all, or will double clicking the specific event automatically add the event handler to the DigitText class instead of the MainWindow class? – Keven M Nov 05 '11 at 19:55
  • If the event is a custom event, you may have to make it a dependency property. If not, it should work just fine for you. – Nate Noonen Nov 05 '11 at 21:35
  • Actually, you can handle the event in both places. As long as you use the control properly and don't mark e.Handled = true. – Nate Noonen Nov 05 '11 at 21:52