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

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

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

Windows 10は、マイクロソフト社がリリースしたOSです。Modern UIを標準画面にした8.1から、10では再びデスクトップ主体に戻され、UIも変更されています。PCやスマホ、タブレットなど様々なデバイスに幅広く対応していることが特徴です。

Windows 7

Microsoft Windows 7は過去にリリースされたMicrosoft WindowsのOSであり、Windows8の1代前です。2009年の7月にリリースされ販売されました。Windows7の前はWindowsVistaで、その更に3年前にリリースされました。

XAML

XAML(Extensible Application Markup Language)はWPF、Silverlight、Windows PhoneそしてWindows Store appsでユーザーインターフェースを定義するために使われるXML言語です。

WPF

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

Q&A

解決済

1回答

1632閲覧

WPFアプリのフォーカス制御について

koshiro_

総合スコア11

Windows 10

Windows 10は、マイクロソフト社がリリースしたOSです。Modern UIを標準画面にした8.1から、10では再びデスクトップ主体に戻され、UIも変更されています。PCやスマホ、タブレットなど様々なデバイスに幅広く対応していることが特徴です。

Windows 7

Microsoft Windows 7は過去にリリースされたMicrosoft WindowsのOSであり、Windows8の1代前です。2009年の7月にリリースされ販売されました。Windows7の前はWindowsVistaで、その更に3年前にリリースされました。

XAML

XAML(Extensible Application Markup Language)はWPF、Silverlight、Windows PhoneそしてWindows Store appsでユーザーインターフェースを定義するために使われるXML言語です。

WPF

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

0グッド

0クリップ

投稿2018/04/17 07:10

編集2018/04/17 08:03

前提・実現したいこと

WPFアプリ開発しています。
テキストボックスを継承し、テキストボックス+ボタンを持たせたカスタムコントロールを作成しました。
Windows7では、カスタムコントロール内のテキストボックスにフォーカスが当たっている時にTabキー押下で、ボタンに移動しますが、
Windows10では、ボタンの次のコントロールにフォーカスが移ります。
画面に以下の様なコントロールが並んでいた場合、Win7とWin10での動作が以下の様に異なります。
・テキストボックスA
・カスタムコントロールB
・テキストボックスB

 Windows7の遷移
テキストボックスA
→カスタムコントロールB(テキストボックス)
→カスタムコントロールB(ボタン)
→テキストボックスB

 Windows10の遷移
テキストボックスA
→カスタムコントロールB(テキストボックス)
→テキストボックスB

shift+Tabで戻る際も同様に上記の逆順で遷移します。
Windows10の動作をWindows7に合わせたいと考えていますが、
カスタムコントロール内で無理やりフォーカス制御を行うぐらいしか思いつきません。
何か良い解決策をご存知の方がいましたら、教えて頂けないでしょうか?

 

該当のソースコード

App.xaml

<Application x:Class="FocusSample.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="Views\MainWindow.xaml" Startup="Application_Startup"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/FocusSample;component/Views/ButtonTextBox/ButtonTextBox.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application>

App.xaml.cs

C#

1using System; 2using System.Collections.Generic; 3using System.Configuration; 4using System.Data; 5using System.Linq; 6using System.Windows; 7 8namespace FocusSample { 9 /// <summary> 10 /// App.xaml の相互作用ロジック 11 /// </summary> 12 public partial class App : Application { 13 private void Application_Startup(object sender, StartupEventArgs e) { 14 } 15 } 16} 17

MainWindow.xaml

<Window x:Class="FocusSample.Views.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:ei="http://schemas.microsoft.com/expression/2010/interactions" xmlns:l="http://schemas.livet-mvvm.net/2011/wpf" xmlns:v="clr-namespace:FocusSample.Views" Title="MainWindow" Height="350" Width="525" xmlns:local="clr-namespace:FocusSample.Views.ButtonTextBox"> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <TextBox Height="30"/> <local:ButtonTextBox Grid.Row="1" Focusable="True" Height="30"/> <TextBox Grid.Row="2" Height="30"/> </Grid> </Window>

MainWindow.xaml.cs

C#

1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Windows; 6using System.Windows.Controls; 7using System.Windows.Data; 8using System.Windows.Documents; 9using System.Windows.Input; 10using System.Windows.Media; 11using System.Windows.Media.Imaging; 12using System.Windows.Navigation; 13using System.Windows.Shapes; 14 15namespace FocusSample.Views { 16 /// <summary> 17 /// MainWindow.xaml の相互作用ロジック 18 /// </summary> 19 public partial class MainWindow : Window { 20 public MainWindow() { 21 InitializeComponent(); 22 } 23 } 24}

ButtonTextBox.cs

C#

1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Windows; 6using System.Windows.Controls; 7using System.Windows.Data; 8using System.Windows.Documents; 9using System.Windows.Input; 10using System.Windows.Media; 11using System.Windows.Media.Imaging; 12using System.Windows.Navigation; 13using System.Windows.Shapes; 14 15namespace FocusSample.Views.ButtonTextBox { 16 public class ButtonTextBox : TextBox { 17 /// <summary> 18 /// コンストラクタ 19 /// </summary> 20 static ButtonTextBox() { 21 DefaultStyleKeyProperty.OverrideMetadata(typeof(ButtonTextBox), new FrameworkPropertyMetadata(typeof(ButtonTextBox))); 22 } 23 24 25 /// <summary> 26 /// ボタンのキャプション 27 /// </summary> 28 public string ButtonContent { 29 get { return (string)GetValue(ButtonContentProperty); } 30 set { SetValue(ButtonContentProperty, value); } 31 } 32 33 public static readonly DependencyProperty ButtonContentProperty = 34 DependencyProperty.Register("ButtonContent", typeof(string), typeof(ButtonTextBox), new UIPropertyMetadata(string.Empty)); 35 36 37 /// <summary> 38 /// ボタンの押されたときのコマンド 39 /// </summary> 40 public ICommand Command { 41 get { return (ICommand)GetValue(CommandProperty); } 42 set { SetValue(CommandProperty, value); } 43 } 44 45 // Using a DependencyProperty as the backing store for Command. This enables animation, styling, binding, etc... 46 public static readonly DependencyProperty CommandProperty = 47 DependencyProperty.Register("Command", typeof(ICommand), typeof(ButtonTextBox)); 48 49 50 51 /// <summary> 52 /// コマンパラメータ 53 /// </summary> 54 public object CommandParameter { 55 get { return (object)GetValue(CommandParameterProperty); } 56 set { SetValue(CommandParameterProperty, value); } 57 } 58 59 // Using a DependencyProperty as the backing store for CommandParameter. This enables animation, styling, binding, etc... 60 public static readonly DependencyProperty CommandParameterProperty = 61 DependencyProperty.Register("CommandParameter", typeof(object), typeof(ButtonTextBox)); 62 63 64 65 public bool IsButtonEnabled { 66 get { return (bool)GetValue(IsButtonEnabledProperty); } 67 set { SetValue(IsButtonEnabledProperty, value); } 68 } 69 70 // Using a DependencyProperty as the backing store for IsButtonEnabled. This enables animation, styling, binding, etc... 71 public static readonly DependencyProperty IsButtonEnabledProperty = 72 DependencyProperty.Register("IsButtonEnabled", typeof(bool), typeof(ButtonTextBox), new UIPropertyMetadata(true)); 73 74 75 } 76} 77 78

ButtonTextBox.xaml

C#

1<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 2 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 3 xmlns:local="clr-namespace:FocusSample.Views.ButtonTextBox"> 4 <Style TargetType="{x:Type local:ButtonTextBox}" BasedOn="{StaticResource {x:Type TextBox}}"> 5 <Setter Property="Template"> 6 <Setter.Value> 7 <ControlTemplate TargetType="{x:Type local:ButtonTextBox}"> 8 <Border Background="{TemplateBinding Background}" 9 BorderBrush="{TemplateBinding BorderBrush}" 10 BorderThickness="{TemplateBinding BorderThickness}" 11 Margin="{TemplateBinding Margin}" 12 Padding="{TemplateBinding Padding}"> 13 <Grid> 14 <Grid.ColumnDefinitions> 15 <ColumnDefinition/> 16 <ColumnDefinition Width="Auto"/> 17 </Grid.ColumnDefinitions> 18 <ScrollViewer x:Name="PART_ContentHost" Focusable="False" VerticalAlignment="{TemplateBinding VerticalAlignment}"/> 19 <Button Grid.Column="1" 20 VerticalAlignment="Center" 21 Command="{TemplateBinding Command}" 22 CommandParameter="{TemplateBinding CommandParameter}"> 23 <Button.Style> 24 <Style TargetType="Button"> 25 <Setter Property="Focusable" Value="True"/> 26 <Setter Property="Content" Value="..."/> 27 </Style> 28 </Button.Style> 29 </Button> 30 </Grid> 31 </Border> 32 </ControlTemplate> 33 </Setter.Value> 34 </Setter> 35 </Style> 36</ResourceDictionary>

補足情報(FW/ツールのバージョンなど)

.NET Framework 4

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

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

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

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

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

Zuishin

2018/04/17 07:14

調査の前段階として、本当に同じバージョンかどうか確かめてください。具体的には新バージョンとわかる見た目を持たせたものを用意し、Win7 と 10 の両方にインストールし、挙動が違うかどうか確かめてください。
koshiro_

2018/04/17 07:30

ご回答ありがとうございます。対象のプログラムはソース管理サーバーにて管理された最新のソースをWin10およびWin7にて取得して確認しております。また、複数の開発者で事象が再現されるため、バージョンの差異は無いかと考えております。
Zuishin

2018/04/17 07:32

不十分だから書いています。同じバイナリーを配布して確かめてください。
koshiro_

2018/04/17 08:05

1からサンプルアプリケーションを作成して、Win7とWin10で挙動が異なる事を確認しました。質問欄にはサンプルのソースと差し替えさえて頂きました。
Zuishin

2018/04/17 08:17

ButtonTextBox.xaml は ResourceDictionary ですか? UserControl もあるのですか?
koshiro_

2018/04/17 08:19

ButtonTextBox.xaml は ResourceDictionary です。UserControl は作成しておりません。
guest

回答1

0

ベストアンサー

試しにButtonTextBox.xamlの
<Grid/>
<Grid KeyboardNavigation.TabNavigation="Local"/>にしてみてください。

投稿2018/04/17 08:30

hihijiji

総合スコア4150

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

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

koshiro_

2018/04/17 08:37

ご回答ありがとうございます。 Gridに実装した所挙動は変わりませんでしたが、 MainWindow.xaml の local:ButtonTextBox に対して KeyboardNavigation.TabNavigation="Local" を追加した所Win10でもフォーカスが当たる様になりました。 非常に助かりました。もう少し検証を行ってみたいと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問