###前提・実現したいこと
こんにちは。
DataGridTemplateColumn.CellTemplate/.CellEditingTemplateを使用した列で
IMEオンの時にName列で入力を行うと、下記の画像のように左上に入力が行ってしまいます。
ID列のように入力時に編集モードへ切り替えたいです。
KeyDownイベントも試しましたが、うまくいきませんでした。
いい方法をご教授いただけないでしょうか。宜しくお願いします。
###該当のソースコード
<!-- MainWindow.xaml --> <Window x:Class="WpfApplication1.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:WpfApplication1" mc:Ignorable="d" Title="MainWindow" Height="400" Width="400" WindowStartupLocation="CenterScreen" InputMethod.PreferredImeState="On"> <Window.Resources> <local:MainWindowViewModel x:Key="ViewModel"/> </Window.Resources> <Window.DataContext> <StaticResource ResourceKey="ViewModel"/> </Window.DataContext> <DataGrid x:Name="dataGrid1" RowHeight="40" ItemsSource="{Binding HogeCollection}" AutoGenerateColumns="False" SelectionUnit="Cell"> <DataGrid.Columns> <DataGridTextColumn Header="ID" Binding="{Binding ID}" Width="150"/> <DataGridTemplateColumn Header="Name" Width="150"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding Name}" KeyDown="TextBlock_KeyDown"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> <DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <TextBox Text="{Binding Name}"/> </DataTemplate> </DataGridTemplateColumn.CellEditingTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> </Window>
/// MainWindow.xaml.cs using System.Windows; using System.Windows.Input; namespace WpfApplication1 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void TextBlock_KeyDown(object sender, KeyEventArgs e) { dataGrid1.BeginEdit(); } } }
/// MainWindowViewModel.cs using System.Collections.Generic; namespace WpfApplication1 { internal class Hoge { public string ID { get; set; } public string Name { get; set; } } internal class MainWindowViewModel { public List<Hoge> HogeCollection { get; set; } public MainWindowViewModel() { this.HogeCollection = new List<Hoge>() { new Hoge() {ID="1", Name="AAA"}, new Hoge() {ID="2", Name="BBB"}, new Hoge() {ID="3", Name="CCC"}, new Hoge() {ID="4", Name="DDD"}, new Hoge() {ID="5", Name="EEE"}, new Hoge() {ID="6", Name="FFF"}, new Hoge() {ID="7", Name="GGG"}, new Hoge() {ID="8", Name="HHH"}, new Hoge() {ID="9", Name="III"} }; } } }
###補足情報(言語/FW/ツール等のバージョンなど)
VisualStudio Pro 2015 FrameWork 4.5.2
OS Windows 10 Pro
あなたの回答
tips
プレビュー