質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
.NET Core

.NET Coreは、マネージソフトウェアフレームワークでオープンソースで実装されています。クロスプラットフォームを前提に考えられており、Windows/Mac/Linuxで動くアプリケーションを作成することが可能です。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

解決済

2回答

2632閲覧

[WPF]【ダイアログ画面】【ユーザーコントロール内で】 エンターキーでフォーカスを移動できない

zin123

総合スコア11

.NET Core

.NET Coreは、マネージソフトウェアフレームワークでオープンソースで実装されています。クロスプラットフォームを前提に考えられており、Windows/Mac/Linuxで動くアプリケーションを作成することが可能です。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

0グッド

0クリップ

投稿2020/05/19 17:01

編集2020/05/20 03:04

前提・実現したいこと

[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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Zuishin

2020/05/19 23:56 編集

> (FocusManager.GetFocusedElement(this) as System.Windows.FrameworkElement) この代わりに this だとどうなりますか? また、TextBox 以外のコントロールだとどうなりますか?
zin123

2020/05/20 00:12

返信ありがとうございます。 (FocusManager.GetFocusedElement(this) as System.Windows.FrameworkElement)?.MoveFocus(new TraversalRequest(direction)); の場合 TextBlock,ComboBox,CheckBoxはKeyDownイベントは発火しますが、フォーカス移動なし。 ButtonはKeyDownイベントは発火しませんでした。 this?.MoveFocus(new TraversalRequest(direction)); の場合 TextBlock,ComboBox,CheckBoxはKeyDownイベントは発火し、Xaml上の先頭コントロールにフォーカスが移動。 ButtonはKeyDownイベントは発火しませんでした。 以上よろしくお願いいたします。
Zuishin

2020/05/20 00:27

ユーザーコントロールが一つのコントロールなので、その中のフォーカスを細かく制御できていないように思います。今は調べられませんが、後で見てみます。 タイトルに「ユーザーコントロール内で」を入れるといいかもしれません。
zin123

2020/05/20 03:07

ご指摘ありがとうございます。 タイトルを編集しました。
guest

回答2

0

ベストアンサー

C#

1 (FocusManager.GetFocusedElement(this) as System.Windows.FrameworkElement)

がnullなんじゃないですかね。

C#

1 (FocusManager.GetFocusedElement(Window.GetWindow(this)) as System.Windows.FrameworkElement)

ではどうですか?
※未検証ですが。。。

投稿2020/05/20 01:32

ebiryo

総合スコア797

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

zin123

2020/05/20 03:09

返信ありがとうございます。 ご指摘の通り、(FocusManager.GetFocusedElement(this) as System.Windows.FrameworkElement)がnullでした。 (FocusManager.GetFocusedElement(Window.GetWindow(this)) as System.Windows.FrameworkElement) に修正したところ、フォーカスを移動できました! ありがとうございました!
guest

0

意味的にはこれで十分でしょうか。

cs

1var element = Keyboard.FocusedElement as UIElement; 2element?.MoveFocus(new TraversalRequest(direction));

とか

cs

1var element = e.OriginalSource as UIElement; 2element?.MoveFocus(new TraversalRequest(direction));

cs

1var element = FocusManager.GetFocusedElement(this) as UIElement; 2element?.MoveFocus(new TraversalRequest(direction));

としたい場合はFocusScopeの設定がいるようです(よくわかってません

xml

1<UserControl FocusManager.IsFocusScope="True" />

or

cs

1public TestControl() 2{ 3 InitializeComponent(); 4 FocusManager.SetIsFocusScope(this, true); 5}

フォーカスの概要 - WPF | Microsoft Docs

投稿2020/05/20 09:08

編集2023/07/21 14:05
TN8001

総合スコア9321

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問