Создайте текстовое поле и обработайте его в событии TextChanged
<TextBox x:Name="money" Text="00.00" TextChanged="TextBox_TextChanged"/>
и код
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (money.Text.Length == 3)
{
money.TextChanged -= TextBox_TextChanged;
money.Text = money.Text.Replace("00.", "00.0");
money.CaretIndex = money.Text.Length;
money.TextChanged += TextBox_TextChanged;
}
if (money.Text.Length == 1)
{
money.TextChanged -= TextBox_TextChanged;
if(money.Text.Contains(".")) money.Text = money.Text.Replace(".", "");
money.Text = money.Text.Insert(0, "00.0");
money.CaretIndex = money.Text.Length;
money.TextChanged += TextBox_TextChanged;
}
else if (money.Text.StartsWith("00.0") && money.Text.Length == 6)
{
money.TextChanged -= TextBox_TextChanged;
money.Text = money.Text.Replace("00.0", "00.");
money.CaretIndex = money.Text.Length;
money.TextChanged += TextBox_TextChanged;
}
else if (money.Text.StartsWith("00.") && money.Text.Length == 6)
{
money.TextChanged -= TextBox_TextChanged;
money.Text = money.Text.Replace("00.", "0");
money.Text = money.Text.Insert(money.Text.Length - 2, ".");
money.CaretIndex = money.Text.Length;
money.TextChanged += TextBox_TextChanged;
}
else if (money.Text.StartsWith("0") && money.Text.Length == 6)
{
money.TextChanged -= TextBox_TextChanged;
money.Text = money.Text.Replace("0", "");
if(money.Text.Contains(".")) money.Text = money.Text.Replace(".", "");
money.Text = money.Text.Insert(money.Text.Length - 2, ".");
money.CaretIndex = money.Text.Length;
money.TextChanged += TextBox_TextChanged;
}
else if(!money.Text.StartsWith("0"))
{
money.TextChanged -= TextBox_TextChanged;
if (money.Text.Length == 3 && money.Text.Contains(".")) money.Text = money.Text.Insert(0, "00");
if(money.Text.Length == 4 && money.Text.Contains(".")) money.Text = money.Text.Insert(0, "0");
if(money.Text.Length == 1 && !money.Text.Contains(".")) money.Text = money.Text.Insert(0, "00.0");
if(money.Text.Length == 2 && !money.Text.Contains(".")) money.Text = money.Text.Insert(0, "00.");
if (money.Text.Length == 3 && !money.Text.Contains(".")) money.Text = money.Text.Insert(0, "0");
if (money.Text.Contains(".")) money.Text = money.Text.Replace(".", "");
money.Text = money.Text.Insert(money.Text.Length - 2, ".");
money.CaretIndex = money.Text.Length;
money.TextChanged += TextBox_TextChanged;
}
else
{
money.TextChanged -= TextBox_TextChanged;
if (money.Text.Length == 3 && money.Text.Contains(".")) money.Text = money.Text.Insert(0, "00");
if (money.Text.Length == 4 && money.Text.Contains(".")) money.Text = money.Text.Insert(0, "0");
if (money.Text.Length == 1 && !money.Text.Contains(".")) money.Text = money.Text.Insert(0, "00.0");
if (money.Text.Length == 2 && !money.Text.Contains(".")) money.Text = money.Text.Insert(0, "00.");
if (money.Text.Length == 3 && !money.Text.Contains(".")) money.Text = money.Text.Insert(0, "0");
if (money.Text.Contains(".")) money.Text = money.Text.Replace(".", "");
money.Text = money.Text.Insert(money.Text.Length - 2, ".");
money.CaretIndex = money.Text.Length;
money.TextChanged += TextBox_TextChanged;
}
}