3

Is it possible to style/template a TextBox so that input fills its value from right to left? My question is not related to the Arabic writing - I'm trying to make a textbox for a currency field, so that when a user types '12' - the value becomes '0.12'. C#/WPF/MVVM project here

Alexey Titov
  • 235
  • 2
  • 9

3 Answers3

4

Use FlowDirection.

<TextBox Text="" FlowDirection="RightToLeft" />

For more info check this link Bidirectional Features in WPF Overview

Hidamax
  • 41
  • 4
1

Did you try HorizontalContentAlignment? It should work for you.

<TextBox HorizontalContentAlignment="Right" Text="6999958"></TextBox>

For converting the value '12' to '0.12', please use a converter like

<TextBox HorizontalContentAlignment="Right" Text="6999958" Converter={Binding CurrencyConverter}></TextBox>

And here goes the converter code :

public class CurrencyConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var intValue = int.Parse(value.ToString());
        var result = 0;
        try
        {
            result = intValue/100;
        }
        catch (Exception)
        {

        }
        return result;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
ViVi
  • 4,339
  • 8
  • 29
  • 52
  • Thanks, this one works for a TextBox. Any chance you could advise a solution for a TextBlock (DataGridColumn.CellStyle) too? TextBlock doesn't have HorizontalContentAlignment property. – Alexey Titov Aug 23 '16 at 07:40
  • Use `TextAlignment` property for textblock. But why do you need it for a textblock, where a user cannot input text. Could you try this for textblock? `` – ViVi Aug 23 '16 at 08:11
  • Yes, thanks, very close to what I want. A TextBlock - because thats DataGridColumn.CellStyle element - when a user clicks on a cell for edit - it becomes a TextBox. If I make that TextBox initially - DataGrid doesn't look nice - and I dont want it to be a collection of TextBoxes that accept cursor at once – Alexey Titov Aug 24 '16 at 10:25
0

Tried multiple solutions here on SO, and IMHO - this one is the best for currency formatting in TextBox. This solution formats the input in currency value, and also accepts only numbers and specified divider (which is customizable in KeyPress event handler). Just give a try, easy to implement and works great for this case ( Supports the format in specific culture rather than your current computer's culture)

private void textBox_TextChanged(object sender, EventArgs e)
{
    textBox.Text = string.Format(System.Globalization.CultureInfo.GetCultureInfo("id-ID"), "{0:##0.00}", double.Parse(textBox.Text));
}

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.'))
    {
        e.Handled = true;
    }

    // only allow one decimal point
    if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
    {
        e.Handled = true;
    }
}
Community
  • 1
  • 1
Artiom
  • 538
  • 12
  • 25