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

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

新規登録して質問してみよう
ただいま回答率
85.50%
XAML

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

WPF

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

Q&A

解決済

1回答

2122閲覧

[WPF]DataGridにおいて、列名に対するタブフォーカスをさせないようにしたい

Rainbow12345

総合スコア7

XAML

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

WPF

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

0グッド

0クリップ

投稿2018/05/26 12:32

前提・実現したいこと

WPFでアプリケーションを作成しています。
TextBoxにフォーカスを当てた状態でF8キーを押すと、DataGridを表示する別Windowを開くようにしてます。
そのウィンドウを開いた際にTabキーを押すと、DataGridの列名にフォーカスが当たってしまっています。

DataGridの内容としては,
ボタン | Key | Value
という3列で作成しています。
やりたいこととしては、Tabキーを押した際には
ボタンのみにフォーカスが移動するようにしたいです。

お手数ですがご回答いただけたら幸いです。

発生している問題・エラーメッセージ

DataGridでIsTabStop="False"にしても、
Tabキーを押した際フォーカスが移動してしまう。

該当のソースコード

MainWindow.xaml

<Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1" xmlns:views="clr-namespace:WpfApp1.View" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" TextElement.Foreground="{DynamicResource MaterialDesignBody}" TextElement.FontWeight="Regular" TextElement.FontSize="13" TextOptions.TextFormattingMode="Ideal" TextOptions.TextRenderingMode="Auto" Background="{DynamicResource MaterialDesignPaper}" FontFamily="{DynamicResource MaterialDesignFont}"> <Grid> <materialDesign:Card Padding="32" Margin="16"> <TextBox IsReadOnly="true" Style="{StaticResource a}" PreviewKeyDown="OpenDialog">aaaaa</TextBox> </Grid> </Window>

MainWindow.xaml.cs

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using WpfApp1.View; namespace WpfApp1 { /// <summary> /// MainWindow.xaml の相互作用ロジック /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void OpenDialog(object sender, KeyEventArgs e) { if(e.Key == Key.F8) { Console.WriteLine("FunctinoKeyPressed"); DialogWindow.items = new List<DataGridItems> { new DataGridItems{Key = 1, Value ="aaaa"}, new DataGridItems{Key = 2, Value ="bbbb"}, new DataGridItems{Key = 3, Value ="cccc"}, new DataGridItems{Key = 4, Value ="dddd"} }; var win = new DialogWindow(); win.Owner = GetWindow(this); win.SetParent(sender as TextBox); win.ShowDialog(); } } } }

DialogWindow.xaml

<Window x:Class="WpfApp1.View.DialogWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1.View" mc:Ignorable="d" Title="DialogWindow" Height="450" Width="800"> <Grid> <DataGrid Name="dataGrid" HorizontalAlignment="Left" IsReadOnly="True" IsTabStop="False" Height="450" VerticalAlignment="Top" Width="800" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTemplateColumn Header="Button" Width="200"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Button Content="Button" Click="Button_Click" IsTabStop="True"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTextColumn Binding="{Binding Key}" ClipboardContentBinding="{x:Null}" Header="Key" IsReadOnly="True" Width="300"/> <DataGridTextColumn Binding="{Binding Value}" ClipboardContentBinding="{x:Null}" Header="Value" IsReadOnly="True" Width="300"/> </DataGrid.Columns> </DataGrid> </Grid> </Window>

DialogWindow.xaml.cs

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace WpfApp1.View { /// <summary> /// DialogWindow.xaml の相互作用ロジック /// </summary> public partial class DialogWindow : Window { public static List<DataGridItems> items = new List<DataGridItems>(); public TextBox parentWindow = new TextBox(); public DialogWindow() { InitializeComponent(); this.dataGrid.ItemsSource = items; } public void SetParent(TextBox textBox) { parentWindow = textBox; } private void Button_Click(object sender, RoutedEventArgs e) { DataGridItems o = this.dataGrid.SelectedItem as DataGridItems; parentWindow.Text = o.Value; this.Close(); } } public class DataGridItems { public int Key { get; set; } public string Value { get; set; } } }

試したこと

DialogWindow.xamlのように
一番親の要素で<DataGrid IsTabStop="False">として、
子要素の<Button IsTabStop="True">とすればいいのかな、
と思い実装してみましたがうまくいきませんでした。

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

WPF
VisualStudio 2017 Community

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

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

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

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

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

guest

回答1

0

ベストアンサー

xamlへ下記記述を追加すると私の環境では動作しましたがいかがでしょうか。

DialogWindow.xaml

<Window x:Class="WpfApp1.View.DialogWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1.View" mc:Ignorable="d" Title="DialogWindow" Height="450" Width="800"> <!-- ResourcesにDataGridCellへ適用するスタイルを追加 --> <Window.Resources> <Style x:Key="UnTabStopCell" TargetType="{x:Type DataGridCell }"> <Setter Property="IsTabStop" Value="False"/> </Style> </Window.Resources> <Grid> <DataGrid Name="dataGrid" HorizontalAlignment="Left" IsReadOnly="True" IsTabStop="False" Height="450" VerticalAlignment="Top" Width="800" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTemplateColumn Header="Button" Width="200"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Button Content="Button" Click="Button_Click" IsTabStop="True"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <!-- フォーカスさせたくないカラムのセルに定義したStyleを適用 --> <DataGridTextColumn Binding="{Binding Key}" ClipboardContentBinding="{x:Null}" Header="Key" IsReadOnly="True" Width="300" CellStyle="{StaticResource UnTabStopCell}"/> <DataGridTextColumn Binding="{Binding Value}" ClipboardContentBinding="{x:Null}" Header="Value" IsReadOnly="True" Width="300" CellStyle="{StaticResource UnTabStopCell}"/> </DataGrid.Columns> </DataGrid> </Grid> </Window>

投稿2018/05/29 00:37

ponpu1601

総合スコア166

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

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

Rainbow12345

2018/05/30 08:53

ありがとうございました! 上の記述で無事ボタンのみにフォーカスを当てることができました。
ponpu1601

2018/05/30 09:23

お力になれたようで何よりです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問