Click here to Skip to main content
15,898,134 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: scrollbar Pin
Pete O'Hanlon30-Mar-11 22:28
mvePete O'Hanlon30-Mar-11 22:28 
GeneralRe: scrollbar Pin
arkiboys30-Mar-11 22:48
arkiboys30-Mar-11 22:48 
GeneralRe: scrollbar Pin
Pete O'Hanlon30-Mar-11 23:04
mvePete O'Hanlon30-Mar-11 23:04 
QuestionNavigating to a specific Panorama item Pin
Paul Harsent30-Mar-11 12:24
Paul Harsent30-Mar-11 12:24 
QuestionTelerik RadRadioButton Hover Effect Pin
Kevin Marois30-Mar-11 11:29
professionalKevin Marois30-Mar-11 11:29 
AnswerRe: Telerik RadRadioButton Hover Effect Pin
SledgeHammer0130-Mar-11 11:42
SledgeHammer0130-Mar-11 11:42 
QuestionWPF Entering Currency Pin
_Maxxx_30-Mar-11 2:10
professional_Maxxx_30-Mar-11 2:10 
AnswerRe: WPF Entering Currency Pin
Pete O'Hanlon30-Mar-11 2:26
mvePete O'Hanlon30-Mar-11 2:26 
You need an attached behavior for this. I wrote one for Goldlight that effectively looks like this:
C#
namespace Goldlight.Behaviors
{
  using System.Windows;
  using System.Windows.Interactivity;
  using System.Windows.Input;
  using System.Windows.Controls;
  using System.Text.RegularExpressions;

  public class NumericTextBoxBehavior : Behavior<TextBox>
  {
    #region Overrides
    protected override void OnAttached()
    {
      base.OnAttached();
      AssociatedObject.PreviewTextInput += AssociatedObject_PreviewTextInput;
      DataObject.AddPastingHandler(AssociatedObject, OnClipboardPaste);
    }

    protected override void OnDetaching()
    {
      AssociatedObject.PreviewTextInput -= AssociatedObject_PreviewTextInput;
      DataObject.RemovePastingHandler(AssociatedObject, OnClipboardPaste);
    }
    #endregion

    #region Private Methods
    private void AssociatedObject_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
      var tb = AssociatedObject;

      e.Handled = !Validate(e.Text);
    }


    private void OnClipboardPaste(object sender, DataObjectPastingEventArgs e)
    {
      var tb = sender as TextBox;
      string text = e.SourceDataObject.GetData(e.FormatToApply) as string;

      if (!string.IsNullOrEmpty(text) && !Validate(text))
      {
        e.CancelCommand();
      }
    }

    private bool Validate(string newContent)
    {
      TextBox tb = AssociatedObject;
      string testString = string.Empty;
      string pre = string.Empty;
      string post = string.Empty;
      // replace selection with new text.
      if (!string.IsNullOrEmpty(tb.SelectedText))
      {
        pre = tb.Text.Substring(0, tb.SelectionStart);
        post = tb.Text.Substring(tb.SelectionStart + tb.SelectionLength,
          tb.Text.Length - (tb.SelectionStart + tb.SelectionLength));
      }
      else
      {
        pre = tb.Text.Substring(0, tb.CaretIndex);
        post = tb.Text.Substring(tb.CaretIndex, tb.Text.Length - tb.CaretIndex);
      }
      testString = pre + newContent + post;

      Regex regExpr = new Regex(@"^([-+]?)(\d*)([,.]?)(\d*)$");
      return (regExpr.IsMatch(testString));
    }
    #endregion
  }
}
The full Goldlight version does quite a bit more with the regex, but this basic logic holds. Now, all you need to do is attach this to your TextBox.

I'm not a stalker, I just know things. Oh by the way, you're out of milk.

Forgive your enemies - it messes with their heads


My blog | My articles | MoXAML PowerToys | Onyx


AnswerRe: WPF Entering Currency [modified] Pin
Pete O'Hanlon30-Mar-11 8:54
mvePete O'Hanlon30-Mar-11 8:54 
GeneralRe: WPF Entering Currency Pin
_Maxxx_30-Mar-11 12:42
professional_Maxxx_30-Mar-11 12:42 
GeneralRe: WPF Entering Currency Pin
Pete O'Hanlon30-Mar-11 13:01
mvePete O'Hanlon30-Mar-11 13:01 
QuestionBinding a Checkbox Column with a Byte Value 0 or 1 [modified] Pin
Vimalsoft(Pty) Ltd29-Mar-11 22:12
professionalVimalsoft(Pty) Ltd29-Mar-11 22:12 
AnswerRe: Binding a Checkbox Column with a Byte Value 0 or 1 Pin
Pete O'Hanlon29-Mar-11 22:52
mvePete O'Hanlon29-Mar-11 22:52 
GeneralRe: Binding a Checkbox Column with a Byte Value 0 or 1 Pin
Vimalsoft(Pty) Ltd30-Mar-11 2:27
professionalVimalsoft(Pty) Ltd30-Mar-11 2:27 
GeneralRe: Binding a Checkbox Column with a Byte Value 0 or 1 Pin
Pete O'Hanlon30-Mar-11 2:30
mvePete O'Hanlon30-Mar-11 2:30 
Questionproblem with cross-domain silverlight and WCF Pin
Yanshof29-Mar-11 5:11
Yanshof29-Mar-11 5:11 
AnswerRe: problem with cross-domain silverlight and WCF Pin
Pete O'Hanlon29-Mar-11 7:03
mvePete O'Hanlon29-Mar-11 7:03 
GeneralRe: problem with cross-domain silverlight and WCF Pin
Yanshof29-Mar-11 21:49
Yanshof29-Mar-11 21:49 
GeneralRe: problem with cross-domain silverlight and WCF Pin
Pete O'Hanlon29-Mar-11 22:25
mvePete O'Hanlon29-Mar-11 22:25 
GeneralRe: problem with cross-domain silverlight and WCF Pin
Yanshof29-Mar-11 23:23
Yanshof29-Mar-11 23:23 
GeneralRe: problem with cross-domain silverlight and WCF Pin
Pete O'Hanlon29-Mar-11 23:33
mvePete O'Hanlon29-Mar-11 23:33 
Questionusercontrol - silverlight 4.0 Pin
arkiboys29-Mar-11 5:04
arkiboys29-Mar-11 5:04 
AnswerRe: usercontrol - silverlight 4.0 Pin
Yanshof29-Mar-11 5:15
Yanshof29-Mar-11 5:15 
AnswerRe: usercontrol - silverlight 4.0 Pin
Mycroft Holmes29-Mar-11 13:04
professionalMycroft Holmes29-Mar-11 13:04 
GeneralRe: usercontrol - silverlight 4.0 Pin
arkiboys30-Mar-11 4:39
arkiboys30-Mar-11 4:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.