前提・実現したいこと
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
回答1件
あなたの回答
tips
プレビュー