Wpf 用户控制数字键盘
我在wpf中制作了一个用户控制数字键盘。我想知道我什么时候在表单上放置了控件。当我按下任何数字按钮时,应将其输入聚焦区域。怎么做 这是我的xaml脚本Wpf 用户控制数字键盘,wpf,custom-controls,Wpf,Custom Controls,我在wpf中制作了一个用户控制数字键盘。我想知道我什么时候在表单上放置了控件。当我按下任何数字按钮时,应将其输入聚焦区域。怎么做 这是我的xaml脚本 在此处输入代码 <RowDefinition Height="1*"/> <RowDefinition Height="1*"/> <RowDefinition Height="1*"/> </Grid.RowDefinitions>
在此处输入代码
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Name="btn2" Content="2" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="1" Grid.Row=" 1" />
<Button Name="btn1" Content="1" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" Grid.Row="1" VerticalAlignment="Stretch" BorderBrush="DarkGray" BorderThickness="4,2,2,8" />
<Button Name="btn3" Content="3" Focusable="False" FontSize="26" Grid.Column="2" Foreground="White" HorizontalAlignment="Stretch" Grid.Row="1" VerticalAlignment="Stretch" />
<Button Name="btn4" Content="4" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" Grid.Row="2" VerticalAlignment="Stretch" />
<Button Name="btn5" Content="5" Focusable="False" FontSize="26" Grid.Column="1" HorizontalAlignment="Stretch" Grid.Row="2" VerticalAlignment="Stretch" />
<Button Name="btn6" Content="6" Focusable="False" FontSize="26" Grid.Column="2" HorizontalAlignment="Stretch" Grid.Row="2" VerticalAlignment="Stretch" />
<Button Name="btn7" Content="7" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" Grid.Row="3" VerticalAlignment="Stretch" />
<Button Name="btn8" Content="8" Focusable="False" FontSize="26" Grid.Column="1" HorizontalAlignment="Stretch" Grid.Row="3" VerticalAlignment="Stretch" />
<Button Name="btn9" Content="9" Focusable="False" FontSize="26" Grid.Column="2" HorizontalAlignment="Stretch" Grid.Row="3" VerticalAlignment="Stretch" />
<Button Name="btn0" Content="0" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" Grid.Row="4" VerticalAlignment="Stretch" />
<Button Name="btn00" Content="00" Focusable="False" FontSize="26" Grid.Column="1" HorizontalAlignment="Stretch" Grid.Row="4" VerticalAlignment="Stretch" />
<Button Name="btn_dot" Content="." Focusable="False" FontSize="26" Grid.Column="2" HorizontalAlignment="Stretch" Grid.Row="4" VerticalAlignment="Stretch" />
<Button Name="btn_temp1" Content="Button" Focusable="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<Button Name="btn_temp2" Content="Button" Focusable="False" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<Button Name="btn_temp3" Content="Button" Focusable="False" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</Grid>
``您应该将事件处理程序附加到,以处理
UserControl
构造函数中WPF中的按键,或加载的事件处理程序:
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Name="btn2" Content="2" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="1" Grid.Row=" 1" />
<Button Name="btn1" Content="1" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" Grid.Row="1" VerticalAlignment="Stretch" BorderBrush="DarkGray" BorderThickness="4,2,2,8" />
<Button Name="btn3" Content="3" Focusable="False" FontSize="26" Grid.Column="2" Foreground="White" HorizontalAlignment="Stretch" Grid.Row="1" VerticalAlignment="Stretch" />
<Button Name="btn4" Content="4" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" Grid.Row="2" VerticalAlignment="Stretch" />
<Button Name="btn5" Content="5" Focusable="False" FontSize="26" Grid.Column="1" HorizontalAlignment="Stretch" Grid.Row="2" VerticalAlignment="Stretch" />
<Button Name="btn6" Content="6" Focusable="False" FontSize="26" Grid.Column="2" HorizontalAlignment="Stretch" Grid.Row="2" VerticalAlignment="Stretch" />
<Button Name="btn7" Content="7" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" Grid.Row="3" VerticalAlignment="Stretch" />
<Button Name="btn8" Content="8" Focusable="False" FontSize="26" Grid.Column="1" HorizontalAlignment="Stretch" Grid.Row="3" VerticalAlignment="Stretch" />
<Button Name="btn9" Content="9" Focusable="False" FontSize="26" Grid.Column="2" HorizontalAlignment="Stretch" Grid.Row="3" VerticalAlignment="Stretch" />
<Button Name="btn0" Content="0" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" Grid.Row="4" VerticalAlignment="Stretch" />
<Button Name="btn00" Content="00" Focusable="False" FontSize="26" Grid.Column="1" HorizontalAlignment="Stretch" Grid.Row="4" VerticalAlignment="Stretch" />
<Button Name="btn_dot" Content="." Focusable="False" FontSize="26" Grid.Column="2" HorizontalAlignment="Stretch" Grid.Row="4" VerticalAlignment="Stretch" />
<Button Name="btn_temp1" Content="Button" Focusable="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<Button Name="btn_temp2" Content="Button" Focusable="False" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<Button Name="btn_temp3" Content="Button" Focusable="False" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</Grid>
PreviewKeyDown += YourKeyEventHandler;
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Name="btn2" Content="2" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="1" Grid.Row=" 1" />
<Button Name="btn1" Content="1" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" Grid.Row="1" VerticalAlignment="Stretch" BorderBrush="DarkGray" BorderThickness="4,2,2,8" />
<Button Name="btn3" Content="3" Focusable="False" FontSize="26" Grid.Column="2" Foreground="White" HorizontalAlignment="Stretch" Grid.Row="1" VerticalAlignment="Stretch" />
<Button Name="btn4" Content="4" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" Grid.Row="2" VerticalAlignment="Stretch" />
<Button Name="btn5" Content="5" Focusable="False" FontSize="26" Grid.Column="1" HorizontalAlignment="Stretch" Grid.Row="2" VerticalAlignment="Stretch" />
<Button Name="btn6" Content="6" Focusable="False" FontSize="26" Grid.Column="2" HorizontalAlignment="Stretch" Grid.Row="2" VerticalAlignment="Stretch" />
<Button Name="btn7" Content="7" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" Grid.Row="3" VerticalAlignment="Stretch" />
<Button Name="btn8" Content="8" Focusable="False" FontSize="26" Grid.Column="1" HorizontalAlignment="Stretch" Grid.Row="3" VerticalAlignment="Stretch" />
<Button Name="btn9" Content="9" Focusable="False" FontSize="26" Grid.Column="2" HorizontalAlignment="Stretch" Grid.Row="3" VerticalAlignment="Stretch" />
<Button Name="btn0" Content="0" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" Grid.Row="4" VerticalAlignment="Stretch" />
<Button Name="btn00" Content="00" Focusable="False" FontSize="26" Grid.Column="1" HorizontalAlignment="Stretch" Grid.Row="4" VerticalAlignment="Stretch" />
<Button Name="btn_dot" Content="." Focusable="False" FontSize="26" Grid.Column="2" HorizontalAlignment="Stretch" Grid.Row="4" VerticalAlignment="Stretch" />
<Button Name="btn_temp1" Content="Button" Focusable="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<Button Name="btn_temp2" Content="Button" Focusable="False" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<Button Name="btn_temp3" Content="Button" Focusable="False" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</Grid>
您可以从MSDN上的链接页面了解更多信息。我无法完全理解。请详细告诉我。如何添加您所说的上述事件,如果您用VB告诉我,这对我也有帮助对不起,我不写visualbasic,您必须使用联机C#to VB转换器。至于细节,我真的不知道我还能说什么。。。只需将事件处理程序附加到您的UserControl
,如我上面所示。如果您真的不知道如何附加事件处理程序,那么很抱歉,您不应该使用堆栈溢出,因为它假设用户对开发有一些基本的了解。好的,那么请告诉我什么是PreviewKeyDown+=YourKeyEventHandler它的含义以及我必须在哪里编写它。这只是将事件处理程序方法(YourKeyEventHandler
)附加到事件(PreviewKeyDown
)的简写符号.但我不知道如何让焦点控件添加在KeyEventHandler中按下的键
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Name="btn2" Content="2" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="1" Grid.Row=" 1" />
<Button Name="btn1" Content="1" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" Grid.Row="1" VerticalAlignment="Stretch" BorderBrush="DarkGray" BorderThickness="4,2,2,8" />
<Button Name="btn3" Content="3" Focusable="False" FontSize="26" Grid.Column="2" Foreground="White" HorizontalAlignment="Stretch" Grid.Row="1" VerticalAlignment="Stretch" />
<Button Name="btn4" Content="4" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" Grid.Row="2" VerticalAlignment="Stretch" />
<Button Name="btn5" Content="5" Focusable="False" FontSize="26" Grid.Column="1" HorizontalAlignment="Stretch" Grid.Row="2" VerticalAlignment="Stretch" />
<Button Name="btn6" Content="6" Focusable="False" FontSize="26" Grid.Column="2" HorizontalAlignment="Stretch" Grid.Row="2" VerticalAlignment="Stretch" />
<Button Name="btn7" Content="7" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" Grid.Row="3" VerticalAlignment="Stretch" />
<Button Name="btn8" Content="8" Focusable="False" FontSize="26" Grid.Column="1" HorizontalAlignment="Stretch" Grid.Row="3" VerticalAlignment="Stretch" />
<Button Name="btn9" Content="9" Focusable="False" FontSize="26" Grid.Column="2" HorizontalAlignment="Stretch" Grid.Row="3" VerticalAlignment="Stretch" />
<Button Name="btn0" Content="0" Focusable="False" FontSize="26" HorizontalAlignment="Stretch" Grid.Row="4" VerticalAlignment="Stretch" />
<Button Name="btn00" Content="00" Focusable="False" FontSize="26" Grid.Column="1" HorizontalAlignment="Stretch" Grid.Row="4" VerticalAlignment="Stretch" />
<Button Name="btn_dot" Content="." Focusable="False" FontSize="26" Grid.Column="2" HorizontalAlignment="Stretch" Grid.Row="4" VerticalAlignment="Stretch" />
<Button Name="btn_temp1" Content="Button" Focusable="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<Button Name="btn_temp2" Content="Button" Focusable="False" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<Button Name="btn_temp3" Content="Button" Focusable="False" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</Grid>