我有一个带有2个TextBox的Orve TabIndex 0,1的WPF窗口,我想在按Enter键时自动将焦点从第一个TextBox移动到第二个TextBox。 我使用MVVM Light。
备注:此帖子不重复。这里我不使用事件处理程序的经典方法,而是使用MVVM模式,因为你知道在视图中不允许使用Code Behind。
我找到了解决方案,但我不知道它是否尊重MVVM原则。 代码如下:
视图
<Window x:Class="WpfMMVLight2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:cmd ="http://www.galasoft.ch/mvvmlight"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding MainViewModel}">
<Grid FocusManager.FocusedElement="{Binding ElementName=tb1}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="70"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Text="Text 1" VerticalAlignment="Center"/>
<TextBox x:Name="tb1" Grid.Column="1" VerticalAlignment="Center" Margin="5">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<cmd:EventToCommand PassEventArgsToCommand="True"
Command ="{Binding KeyDownCommand, Mode=OneWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
<TextBlock Grid.Column="0" Grid.Row="1" Text="Text 2" VerticalAlignment="Center"/>
<TextBox x:Name="tb2" Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" Margin="5">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<cmd:EventToCommand PassEventArgsToCommand="True"
Command ="{Binding KeyDownCommand, Mode=OneWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
</Grid>
ViewModel:
private ICommand _keydownCommand;
public ICommand KeyDownCommand
{
get
{
if (_keydownCommand== null)
_keydownCommand= new DelegateCommand<KeyEventArgs>(KeyDownCommandExecute);
return _keydownCommand;
}
}
private void KeyDownCommandExecute(KeyEventArgs e)
{
if (e != null && e.Key == Key.Enter)
{
TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
request.Wrapped = true;
((Control)e.Source).MoveFocus(request);
}
}
}
我不知道是否允许在ViewModel中使用“Control”类
答案 0 :(得分:11)
在使用MVVM时,您可以使用行为:
public class TabOnEnterBehavior : Behavior<TextBox>
{
protected override void OnAttached()
{
AssociatedObject.PreviewKeyDown += AssociatedObject_PreviewKeyDown;
}
private void AssociatedObject_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
var request = new TraversalRequest(FocusNavigationDirection.Next);
request.Wrapped = true;
AssociatedObject.MoveFocus(request);
}
}
protected override void OnDetaching()
{
AssociatedObject.PreviewKeyDown -= AssociatedObject_PreviewKeyDown;
}
}
在你的xaml中:
<TextBox>
<i:Interaction.Behaviors>
<wpfTest:TabOnEnterBehavior />
</i:Interaction.Behaviors>
</TextBox>
答案 1 :(得分:2)
首先向PreviewKeyDown
添加TextBox
个活动:
<TextBox PreviewKeyDown="OnTextBoxKeyDown" />
然后创建TraversalRequest
以将焦点移至下一个项目:
private void OnTextBoxKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
MoveFocus(request);
}
}
修改强>
或者,如果您更喜欢使用命令,则只需在KeyBinding
中设置TextBox
:
<TextBox.InputBindings>
<KeyBinding Key="Enter" Command="{Binding YourCommand}" />
</TextBox.InputBindings>
在ICommand s
Execute`逻辑中添加与上面相同的内容。
此外,同样的问题:How do I simulate a Tab key press when Return is pressed in a WPF application?