前提・実現したいこと
[WPF]【ダイアログ画面】 エンターキーでフォーカスを移動できない
発生している問題・エラーメッセージ
ダイアログ(UserControl)のKeyDownイベントは発火するが、フォーカスが移動しない
該当のソースコード
TestControl.xaml
C#
1<UserControl x:Class="BlankCoreApp11.Views.TestControl" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:prism="http://prismlibrary.com/" 5 prism:ViewModelLocator.AutoWireViewModel="True" 6 KeyDown="UserControl_KeyDown"> 7 <Grid> 8 <StackPanel Width="100"> 9 <TextBox/> 10 <TextBox/> 11 <TextBox/> 12 </StackPanel> 13 </Grid> 14</UserControl>
TestControl.xaml.cs
C#
1using System.Windows.Controls; 2using System.Windows.Forms; 3using System.Windows.Input; 4 5namespace BlankCoreApp11.Views 6{ 7 /// <summary> 8 /// Interaction logic for TestControl 9 /// </summary> 10 public partial class TestControl : System.Windows.Controls.UserControl 11 { 12 public TestControl() 13 { 14 InitializeComponent(); 15 } 16 17 private void UserControl_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) 18 { 19 if (e.Key != Key.Enter) { return; } 20 var direction = Keyboard.Modifiers == ModifierKeys.Shift ? FocusNavigationDirection.Previous : FocusNavigationDirection.Next; 21 (FocusManager.GetFocusedElement(this) as System.Windows.FrameworkElement)?.MoveFocus(new TraversalRequest(direction)); 22 } 23 } 24} 25
MainWindow.xaml
C#
1<Window x:Class="BlankCoreApp11.Views.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:prism="http://prismlibrary.com/" 5 prism:ViewModelLocator.AutoWireViewModel="True" 6 Title="{Binding Title}" Height="350" Width="525" > 7 <Grid> 8 <StackPanel> 9 <Button Content="表示" 10 Command="{Binding TestCommand}"/> 11 </StackPanel> 12 </Grid> 13</Window> 14
MainWindowViewModel.cs
C#
1using BlankCoreApp11.Views; 2using Prism.Mvvm; 3using Prism.Services.Dialogs; 4using Reactive.Bindings; 5using System; 6 7namespace BlankCoreApp11.ViewModels 8{ 9 public class MainWindowViewModel : BindableBase 10 { 11 private string _title = "Prism Application"; 12 public string Title 13 { 14 get { return _title; } 15 set { SetProperty(ref _title, value); } 16 } 17 public ReactiveCommand TestCommand { get; } 18 private IDialogService dialogService; 19 public MainWindowViewModel(IDialogService dialogService) 20 { 21 this.dialogService = dialogService; 22 this.TestCommand = new ReactiveCommand(); 23 this.TestCommand.Subscribe(this.Test); 24 } 25 26 private void Test() 27 { 28 this.dialogService.ShowDialog(nameof(TestControl), null, null); 29 } 30 } 31} 32
App.xaml.cs
C#
1using Prism.Ioc; 2using BlankCoreApp11.Views; 3using System.Windows; 4using BlankCoreApp11.ViewModels; 5 6namespace BlankCoreApp11 7{ 8 /// <summary> 9 /// Interaction logic for App.xaml 10 /// </summary> 11 public partial class App 12 { 13 protected override Window CreateShell() 14 { 15 return Container.Resolve<MainWindow>(); 16 } 17 18 protected override void RegisterTypes(IContainerRegistry containerRegistry) 19 { 20 containerRegistry.RegisterDialog<TestControl, TestControlViewModel>(); 21 } 22 } 23}
補足情報(FW/ツールのバージョンなど)
VS2019
WPF
.Net Core 3.1
Prism.Unity 7.2.0.1422
ReactiveProperty 7.0.1
回答2件
あなたの回答
tips
プレビュー