LoginSignup
5
5

More than 1 year has passed since last update.

WPF DatePickerで、IMEをオフにする。

Last updated at Posted at 2021-12-22

DatePickerのテキストボックスの入力欄で、IMEをオフにする方法。
通常のTextBoxでIMEをオフにするのとは違い、工夫が必要。

XAMLとスタイル

手間がかからない方法。
PreferredImeStateとスタイルを指定。

<DatePicker
  InputMethod.PreferredImeState="Off"
  Style="{StaticResource DatePickerStyle1}" />

DetaPickerのデフォルトのスタイルの PART_TextBox に次を追加

<Style x:Key="DatePickerStyle1" TargetType="{x:Type DatePicker}">
(省略)
<DatePickerTextBox x:Name="PART_TextBox"  (省略)
InputMethod.PreferredImeState="{Binding Path=(InputMethod.PreferredImeState), RelativeSource={RelativeSource TemplatedParent}}" />

コードで追加する場合

DatePickerコントロールにスタイルと、PreferredImeStateをセットします。

var datePicker = new DatePicker();
datePicker.Style = (Style)Application.Current.Resources["DatePickerStyle1"];
InputMethod.SetPreferredImeState(datePicker, InputMethodState.Off);
RootStackPanel.Children.Add(datePicker);

コード

.NET Framework 4.7
https://github.com/mikihiro-t/IMEStudy

image.png

参考

XAMLで直接Templateを変更。この場合、"PART_TextBox"は必要では無いようだ。
https://stackoverflow.com/a/27300604/9924249

備考

  • 先人の研究やコードを参考にしました。いろんな方法があったのですが、上記の方法は、まだ記されていないようでした。
  • 物事を解決するのに、様々な方法があるのはいいのですが、どの方法がベストプラクティスか、判断が難しい。

履歴

2021-12-23 IMEを無効 → IMEをオフ に変更。 IMEの有効無効は、SetIsInputMethodEnabledで設定するため。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5