6

Is it possible to remove the currency formatting on a WPF textbox on focus? The app follows MVVM.

<TextBox HorizontalAlignment="Left"
         Height="23"
         Margin="156,264,0,0"
         TextWrapping="Wrap"
         HorizontalContentAlignment="Right"
         Text="{Binding Amount, StringFormat=c, ValidatesOnNotifyDataErrors=True}"
         VerticalAlignment="Top"
         Width="100" />

I need the formatting while the textbox is not having focus. But it needs to be removed only when it having focus to enable easy editing by the user. The reason for removing the $ is that when you tab, the focus is before the $. This means the user has to click again or use the arrow key to move to the digits.

When the user tabs to the above textbox the currency symbol should be removed. Thanks for your help.

Daniel
  • 10,864
  • 22
  • 84
  • 115
isakavis
  • 773
  • 1
  • 12
  • 29

3 Answers3

12

You can write trigger that will be executed when TextBox is focused and at the moment you can change StringFormat.

TextBox style:

<TextBox TextWrapping="Wrap"
         Height="23" Width="200" 
         HorizontalAlignment="Left" HorizontalContentAlignment="Right" VerticalAlignment="Top">
    <TextBox.Style>
        <Style TargetType="TextBox">                   
            <Setter Property="Text" Value="{Binding Amount, StringFormat=C, UpdateSourceTrigger=LostFocus}" />
            <Style.Triggers>
                <Trigger Property="Control.IsFocused" Value="True">
                    <Setter Property="Text" Value="{Binding Amount, StringFormat=F2, UpdateSourceTrigger=LostFocus}" />                            
                </Trigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>
kmatyaszek
  • 19,016
  • 9
  • 60
  • 65
  • This the approach I went with which is similar to http://stackoverflow.com/questions/9910681/textbox-with-currencyformat-and-propertychanged-trigger-doesnt-accept-text-righ – isakavis May 20 '13 at 19:55
0

Remove StringFormat=c.

<TextBox HorizontalAlignment="Left" Height="23" Margin="156,264,0,0" TextWrapping="Wrap" HorizontalContentAlignment="Right"
            Text="{Binding Amount, ValidatesOnNotifyDataErrors=True}" VerticalAlignment="Top" Width="100" />

If that doesn't fix it, there's something being done behind the scenes to your Amount property in your viewmodel that is adding the formatting. Since that code was not part of the question, I can't help you with that unless you edit your post and include the relevant code.

qJake
  • 16,821
  • 17
  • 83
  • 135
  • Spike, I need the formatting in the textbox. But it needs to be removed only when it having focus. – isakavis May 20 '13 at 19:07
  • Why not just put the currency symbol separately before the textbox? That way you don't have to worry about switching the string back and forth on focus like this. -- You can get the current localized currency symbol from information in this post, if you aren't just dealing with $. http://stackoverflow.com/questions/2763128/get-the-currency-from-current-culture – qJake May 20 '13 at 19:09
  • "*Why not just put the currency symbol separately before the textbox?*" or after if the locale changes and currency unit becomes, say €. – mins Jul 22 '19 at 09:35
0

How about using a Property to set the string format instead of specifying the string format in XAML, setting the property value to c when the VM is loaded and changing it to empty string in TextBox GotFocus event handler?

XAML:

StringFormat="{Binding MyStringFormat}"

ViewModel:

private string myStringFormat;
public string MyStringFormat
{
   get { return myStringFormat; }
   set
   {
       myStringFormat= value;
       NotifyPropertyChanged(m => m.MyStringFormat);
   }
}

View:

void MyTextBox_GotFocus(object sender, EventArgs e)
{
    myvm = DataContext as MyViewModel;
    if (myvm == null) return;
    myvm.MyStringFormat = string.Empty;
}

void MyTextBox_LostFocus(object sender, EventArgs e)
{
    myvm = DataContext as MyViewModel;
    if (myvm == null) return;
    myvm.MyStringFormat = "c";
}
Dean Kuga
  • 11,878
  • 8
  • 54
  • 108
  • Dean, Thanks for the detail. I am reviewing the following links http://stackoverflow.com/questions/9910681/textbox-with-currencyformat-and-propertychanged-trigger-doesnt-accept-text-righ & http://stackoverflow.com/questions/5594437/modifying-the-parameters-of-a-textboxs-text-binding-through-the-use-of-a-style – isakavis May 20 '13 at 19:30