1

Topic: [WPF] TextBox an input mask

Greetings to all! There was a task of the job of data input on a mask. And the mask can change depending on what that of dynamic parameters. Not the essence important from what and as, and it is important that StringFormat for a binding does not approach. Is interesting to become which solves such task. Source codes as are applied and successfully gather. After start of compiled application TextBox transforms the text accurately on the given mask. You ask me: well time all works that what for at a forum a post to write? I answer: Yes here not absolutely correctly works. It appears, if for a binding to set UpdateSourceTrigger=PropertyChanged the entered text ignores a mask. The reason consists in that that when the author of article appropriates TextBox. Text the formatted text the input a binding which activates the converter which imports the last word and since to the displayed text at once enters The converter is not engaged in formatting on an output at us the normal not formatted text turns out. For UpdateSourceTrigger=LostFocus such problems certainly are not present. As a result the algorithm of the author is fastened rigidly on value UpdateSourceTrigger what not to eat well! Here if it would be possible to disconnect a binding, in advance saving its parameters, at a stage of change of property Text, and then it "on silent" to include, it would solve a problem. But there is such code strongly beats on a stack. var binding = textBox. GetBindingExpression (TextBox. TextProperty);//we Save a binding textBox. ClearValue (TextBox. TextProperty);//we disconnect a binding textBox. Text = text;//we change property textBox. SetBinding (TextBox. TextProperty, binding. ParentBinding); //we return a binding The matter is that when we return a binding, at once it is activated and action is entered by the converter which launches further all this mechanism and there is the infinite recursion and stack overflow. Actually, how to correct the code of the author, to untie it from value UpdateSourceTrigger?

2

Re: [WPF] TextBox an input mask

Hello, SanyaVB, you wrote: SVB> Greetings to all! SVB> There was a task of the job of data input on a mask. And the mask can change depending on what that of dynamic parameters. Not the essence important from what and as, and it is important that StringFormat for a binding does not approach. SVB> is interesting to become which solves such task. Source codes as are applied and successfully gather. After start of compiled application TextBox transforms the text accurately on the given mask. You ask me: well time all works that what for at a forum a post to write? SVB> I answer: Yes here not absolutely correctly works. It appears, if for a binding to set UpdateSourceTrigger=PropertyChanged the entered text ignores a mask. The reason consists in that that when the author of article appropriates TextBox. Text the formatted text the input a binding which activates the converter which imports the last word and since to the displayed text at once enters The converter is not engaged in formatting on an output at us the normal not formatted text turns out. For UpdateSourceTrigger=LostFocus such problems certainly are not present. SVB> As a result the algorithm of the author is fastened rigidly on value UpdateSourceTrigger what not to eat well! SVB> here if it would be possible to disconnect a binding, in advance saving its parameters, at a stage of change of property Text, and then it "on silent" to include, it would solve a problem. But there is such code strongly beats on a stack. SVB> SVB> var binding = textBox. GetBindingExpression (TextBox. TextProperty);//we Save binding SVB> textBox. ClearValue (TextBox. TextProperty);//we disconnect binding SVB> textBox. Text = text;//we change property SVB> textBox. SetBinding (TextBox. TextProperty, binding. ParentBinding); //we return binding SVB> SVB> The matter is that when we return a binding, at once it is activated and action is entered by the converter which launches further all this mechanism and there is the infinite recursion and stack overflow. SVB> actually how to correct the code of the author to untie it from value UpdateSourceTrigger? Here such crutch://we redefine TextBox public class MyTextBox: TextBox {public static readonly DependencyProperty FormatText_Property = DependencyProperty. Register ("FormatText", typeof (string), typeof (MyTextBox), new UIPropertyMetadata (null)); public MyTextBox () {TextChanged + = MyTextBox_TextChanged;} public string FormatText {get {return (string) GetValue (FormatText_Property);} set {SetValue (FormatText_Property, value);}} private void MyTextBox_TextChanged (object sender, TextChangedEventArgs e) {SetValue (FormatText_Property, Text);} }//a binding: <testEntry:MyTextBox Width = "450" HorizontalContentAlignment = "Right" FormatText = "{Binding Path=TestDataInt, Converter = {StaticResource myconverter}, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" myentry:TextBoxMaskBehavior. Mask = "0:#,0." myentry:TextBoxMaskBehavior. ValueType = "{Binding Path=TestDataInt, Converter = {StaticResource mytypeconverter}, Mode=OneWay}"/> </StackPanel> <StackPanel Height = "Auto" HorizontalAlignment = "Left" Margin = "0" Name = "spDouble" VerticalAlignment = "Top" Width = "Auto" Orientation = "Horizontal"> <Label> Double: </Label> <testEntry:MyTextBox Width = "450" HorizontalContentAlignment = "Right" FormatText = "{Binding Path=TestDataDouble, Converter = {StaticResource myconverter}, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" myentry:TextBoxMaskBehavior. Mask = "0:#,0.00##" myentry:TextBoxMaskBehavior. ValueType = "{Binding Path=TestDataDouble, Converter = {StaticResource mytypeconverter}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/> </StackPanel> <StackPanel Height = "Auto" HorizontalAlignment = "Left" Margin = "0" Name = "spNoNumeric" VerticalAlignment = "Top" Width = "Auto" Orientation = "Horizontal"> <Label> NoNumeric: </Label> <testEntry:MyTextBox Width = "425" HorizontalContentAlignment = "Left" FormatText = "{Binding Path=TestDataNoNumeric, Converter = {StaticResource myconverter}, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" myentry:TextBoxMaskBehavior. Mask = "" myentry:TextBoxMaskBehavior. ValueType = "{Binding Path=TestDataNoNumeric, Converter = {StaticResource mytypeconverter}, Mode=OneWay}"/> Like all works. The only thing  UpdateSourceTrigger depends from TextBoxMaskBehavior. ValueType, therefore if needed it is necessary to specify it explicitly.

3

Re: [WPF] TextBox an input mask

Hello, Qulac, you wrote: Q> Here such crutch: Q> Like all works. The only thing  UpdateSourceTrigger depends from TextBoxMaskBehavior. ValueType, therefore if needed it is necessary to specify it explicitly. Yes, thanks for the answer! It is really operating crutch. It is possible not to train the user in what cases to use FormatText and in what Text, to rewrite a crutch here so: public class MyTextBox: TextBox {public static readonly DependencyProperty FormatText_Property = DependencyProperty. Register ("Text", typeof (string), typeof (MyTextBox), new UIPropertyMetadata (null)); public MyTextBox () {TextChanged + = MyTextBox_TextChanged;} public new string Text {get {return (string) GetValue (FormatText_Property);} set {SetValue (FormatText_Property, value);}} private void MyTextBox_TextChanged (object sender, TextChangedEventArgs e) {SetValue (FormatText_Property, Text);} } Though..... If to refuse from RegisterAttached and all this logic to place in MyTextBox as a matter of fact it already will be not a crutch, and certain  with a title we tell MaskTextBox But if all the same it would be desirable to create attached properties to TextBox... Can eat other variants?

4

Re: [WPF] TextBox an input mask

Hello, SanyaVB, you wrote: SVB> There was a task of the job of data input on a mask. My absolutely serious council: reduce the director of such task to , it is explicitly sick of a complex of god. (To moderators: it is absolute IT  the answer!) I will explain: the mask has been invented 100 years ago that the stupid operator  entered the data and was not mistaken. That is the data source always was the person (it GUI!) . Now sources there can be a sea - , messengers, autofillers of browsers,  and other. And now think, what abusive words depart to  address, which phone "disguised" as "### ## ##", and the operator tries to interpose (and then to modify) the text from the web page "ph. 1234-5678" - yes-yes, with letters and is perfect other separator and at all in that place! Therefore whenever possible DO NOT PRY into how the user enters the data!! They can be absolutely chaotic. All  - is exceptional in the end OR (if it is possible) - simply exclamative tag with a field and  "that is wrong".

5

Re: [WPF] TextBox an input mask

Hello, Kolesiki, you wrote: K> Hello, SanyaVB, you wrote: SVB>> There was a task of the job of data input on a mask. K> my absolutely serious council: reduce the director of such task to , it is explicitly sick of a complex of god. (To moderators: it is absolute IT  the answer!) K> I will explain: the mask has been invented 100 years ago that the stupid operator  entered the data and was not mistaken. That is the data source always was the person (it GUI!) . Now sources there can be a sea - , messengers, autofillers of browsers,  and other. And now think, what abusive words depart to  address, which phone "disguised" as "### ## ##", and the operator tries to interpose (and then to modify) the text from the web page "ph. 1234-5678" - yes-yes, with letters and is perfect other separator and at all in that place! K> therefore whenever possible DO NOT PRY into how the user enters the data!! They can be absolutely chaotic. All  - is exceptional in the end OR (if it is possible) - simply exclamative tag with a field and  "that is wrong". The world multi-colored. For your example it is certainly truly written. But there are cases when it is necessary not to allow to enter at a stage of input what that the left characters and it is beautiful to display them. Your answer explicitly speaks: do not struggle with this task since there is an alternative variant of data input and check. And further result arguments. Your error consists in that that a post  is not necessary. The question was about solutions of a problem of this task namely as it is beautiful to display the entered text on a mask. Article, its author certainly not I has been resulted! By the way it is a lot of such articles... People morons that write such articles??? I all the same come to such judgement that because of impossibility to change behavior of already anchored binding, does a task in view not possible. By the way property TextBox. Text artful enough... Any change generates event TextChanged. I.e. if in this output agent of event to change TextBox. Text it is possible to leave in the infinite recursion. Developers of Microsoft did not want to create method EventToSleep which lulls events, i.e. they are not caused and EventWakeUp which clears up event, but does not appeal, and simply does its by active. textbox. TextChanged - = TextChanged does not approach, since In such a way it is possible  only the method, here if on event methods are signed what that still... That they as will be caused. It is necessary to write a rake: private static readonly object ignorEvent = new object (); private static void SetText (TextBox textBox, string text) {object tag = textBox. Tag; textBox. Tag = ignorEvent; textBox. Text = text; textBox. Tag = tag;} private static void TextChanged (object sender, TextChangedEventArgs e) {var textBox = sender as TextBox; if (textBox == null || textBox. Tag == ignorEvent) {e. Handled = true; return;}//Here what that the logic} By the way at the author of article of such code is not present, and as a result TextChanged fulfills 2 times one and too formatting. By miracle does not get to a recursion... Why??? Can have a look its code. But if to begin something there  that it is possible to run into stack overflow.

6

Re: [WPF] TextBox an input mask

Hello, SanyaVB, you wrote: SVB> your error consists in that that a post  is not necessary. Thanks  that all nonsenses are fixed and  it is impossible! Well here for whom I wrote it? K>> all  - is exceptional in the end OR (if it is possible) - simply exclamative tag with a field and  "that is wrong". In the end. Also I will repeat, the world became more difficult and is far not the fact that all your data will be entered absolutely from the keypad. And how Siri? Copy-Paste? You are responsible for what tomorrow they will not be?

7

Re: [WPF] TextBox an input mask

Hello, SanyaVB, you wrote: SVB> the Question was about solutions of a problem of this task namely as it is beautiful to display the entered text on a mask. Here there is a subtlety: it is possible to FORMAT on a mask, and it is possible  on a mask. It is necessary to mean. For example, phone - it is possible to accept ALL characters, but to put them in a position "@@@ @@ @@". Then in the end  and to show . Such in the image, you have and , and formatting, And DO NOT IRRITATE the USER!

8

Re: [WPF] TextBox an input mask

Hello, Kolesiki, you wrote: K> Hello, SanyaVB, you wrote: SVB>> the Question was about solutions of a problem of this task namely as it is beautiful to display the entered text on a mask. K> here there is a subtlety: it is possible to FORMAT on a mask, and it is possible  on a mask. It is necessary to mean. K> for example, phone - it is possible to accept ALL characters, but to put them in a position "@@@ @@ @@". Then in the end  and to show . Such in the image, you have and , and formatting, And DO NOT IRRITATE the USER! From discharge: ... As that is difficult... And what for to you it??? Do and DO NOT IRRITATE the USER! Very few people practises input of a key of the license of OS Windows, but there is used artful ... It accepts digits and some letters (all alphabet). At input by their automatic machine divides a hyphen. Save  there it is impossible to push what that the left characters.... There it is impossible to enter the left characters from the keypad and there there are no exclamation points and  during input. The key correctness is checked after button click... But initiating check and formatting happens during input or a key insertion! Also what??? Now developers should be abused that did not follow your strategy??? And personally it to be pleasant to me as they made! In my opinion very conveniently! Type not . To enter that that it is not necessary. By the way and ABBY too such  uses... Norton too... The list can be continued further... And all of them oppose to yours: "and what for to you it??? Do and DO NOT IRRITATE the USER!"