我有一个按钮小键盘和一个文本块。现在,我想使用此数字小键盘输入Dollart金额并将其显示在文本块中。例如,如果我按顺序按1234。文本块应显示12.34。如何在c#中执行此操作?我在Internet上阅读了很多示例,但是没有找到与我的情况相符的示例。
<Grid x:Name="Numpad">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<!--buttons Row-->
<Button x:Name="popup_ClrBtn" Grid.Row="0" Content="Clear" FontFamily="Times New Roman" Margin="2" FontSize="15" FontWeight="Bold" Click="ClrBtn_Click"/>
<Button x:Name="popup_AcptBtn" Grid.Row="0" Grid.Column="2" Content="Accpet" FontFamily="Times New Roman" Margin="2" FontSize="15" FontWeight="Bold"/>
<!--Keyboard grid-->
<!--first row of buttons-->
<Button x:Name="btn7" Grid.Column="0" Grid.Row="1" Content="7" HorizontalAlignment="Stretch" FontFamily="Times New Roman" Margin="2" FontSize="15" FontWeight="Bold" Click="numpad_Click"/>
<Button x:Name="btn8" Grid.Column="1" Grid.Row="1" Content="8" HorizontalAlignment="Stretch" FontFamily="Times New Roman" Margin="2" FontSize="15" FontWeight="Bold" Click="numpad_Click" />
<Button x:Name="btn9" Grid.Column="2" Grid.Row="1" Content="9" HorizontalAlignment="Stretch" FontFamily="Times New Roman" Margin="2" FontSize="15" FontWeight="Bold" Click="numpad_Click" />
<!--Second row of buttons-->
<Button x:Name="btn4" Grid.Column="0" Grid.Row="2" Content="4" HorizontalAlignment="Stretch" FontFamily="Times New Roman" Margin="2" FontSize="15" FontWeight="Bold" Click="numpad_Click"/>
<Button x:Name="btn5" Grid.Column="1" Grid.Row="2" Content="5" HorizontalAlignment="Stretch" FontFamily="Times New Roman" Margin="2" FontSize="15" FontWeight="Bold" Click="numpad_Click" />
<Button x:Name="btn6" Grid.Column="2" Grid.Row="2" Content="6" HorizontalAlignment="Stretch" FontFamily="Times New Roman" Margin="2" FontSize="15" FontWeight="Bold" Click="numpad_Click" />
<!--Third row of buttons-->
<Button x:Name="btn1" Grid.Column="0" Grid.Row="3" Content="1" HorizontalAlignment="Stretch" FontFamily="Times New Roman" Margin="2" FontSize="15" FontWeight="Bold" Click="numpad_Click"/>
<Button x:Name="btn2" Grid.Column="1" Grid.Row="3" Content="2" HorizontalAlignment="Stretch" FontFamily="Times New Roman" Margin="2" FontSize="15" FontWeight="Bold" Click="numpad_Click" />
<Button x:Name="btn3" Grid.Column="2" Grid.Row="3" Content="3" HorizontalAlignment="Stretch" FontFamily="Times New Roman" Margin="2" FontSize="15" FontWeight="Bold" Click="numpad_Click" />
<!--Last row of buttons-->
<Button x:Name="btn0" Grid.Column="0" Grid.Row="4" Content="0" HorizontalAlignment="Stretch" FontFamily="Times New Roman" Margin="2" FontSize="15" FontWeight="Bold" Click="numpad_Click"/>
<Button x:Name="btn00" Grid.Column="1" Grid.Row="4" Content="00" HorizontalAlignment="Stretch" FontFamily="Times New Roman" Margin="2" FontSize="15" FontWeight="Bold" Click="numpad_Click" />
<Button x:Name="CancelBtn" Grid.Column="2" Grid.Row="4" Content="Cancel" HorizontalAlignment="Stretch" FontFamily="Times New Roman" Margin="2" FontSize="15" FontWeight="Bold" Click="CancelBtn_Click" Background="#FFF70B0B" />
<Label x:Name="amountlbl" Grid.Row="5">Amount:</Label>
<TextBlock x:Name="amountTb" Grid.Row="5" Grid.Column="1" Grid.ColumnSpan="2"></TextBlock>
</Grid>
答案 0 :(得分:1)
将TextChanged
事件处理程序添加到您的TextBox
:
<TextBox Grid.Row="0" Margin="10" TextChanged="TextBox_TextChanged" />
并根据需要修改以下事件处理程序代码:
private bool suppressTextChanged = false;
private void TextBox_TextChanged(Object sender, TextChangedEventArgs e)
{
int caretIndex = textBox.CaretIndex;
string text = textBox.Text;
int value;
if (int.TryParse(text, out value))
{
int insertPos = text.Length - 2;
if (insertPos > 0)
{
text = text.Insert(insertPos, ".");
suppressTextChanged = true;
textBox.Text = text;
if (caretIndex >= insertPos)
caretIndex++;
textBox.CaretIndex = caretIndex;
suppressTextChanged = false;
}
}
}