1.CallMethodAction
< TextBox Height = "30" Name = "txtUserName" Width = "160" FontSize = "20" VerticalContentAlignment = "Center" Text = "{Binding UserNme}" > < i:Interaction.Triggers > < i:EventTrigger EventName = "KeyDown" > < ei:CallMethodAction TargetObject = "{Binding}" MethodName = "UserNameKeyDown" /> </ i:EventTrigger > </ i:Interaction.Triggers > </ TextBox > |
public void UserNameKeyDown( object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { Login( null ); } } |
2.TabControl
<Window x:Class="wpfApplication" ...
Title="Window1" Height="100" Width="200">
<ScrollViewer>
<TabControl Height="500" Margin="20" SelectionChanged="TabControlSelectionChanged">
<TabItem Header="tabItem1">
<Grid>
<TextBox />
<Grid>
</TabItem>
<TabItem Header="tabItem2">
<Grid>
<TextBox />
<Grid>
</TabItem>
<TabItem Header="tabItem3">
<Grid>
<TextBox />
<Grid>
</TabItem>
</TabControl>
</ScrollViewer>
</Window>
C#代码:
private void TabControlSelectionChanged(object sender, SelectionChangedEventArgs e){
var item = tabControl1.SelectedItem as TabItem;
if (e.Source is TabControl){
item.Focus();
}
}