teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

見直しキャンペーン中

2023/07/22 09:13

投稿

TN8001
TN8001

スコア10104

answer CHANGED
@@ -1,72 +1,72 @@
1
- byoriさんの要件と厳密には合致していないかもしれませんが、ひとつ手軽にできる方法があります。
2
-
3
- 1文字入力ごとに「新しい文字列が古い文字列と同じ文字列で始まっている」もしくは「新しい文字列が古い文字列と同じ文字列で終わっている」時だけ入力を受け付けるようにします。
4
-
5
- ```xaml
6
- <Window
7
- x:Class="Questions277480.MainWindow"
8
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
9
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
10
- Width="800"
11
- Height="450">
12
- <Grid>
13
- <TextBox
14
- AcceptsReturn="True"
15
- AcceptsTab="True"
16
- Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}" />
17
- </Grid>
18
- </Window>
19
- ```
20
-
21
- ```C#
22
- using System.ComponentModel;
23
- using System.Runtime.CompilerServices;
24
- using System.Windows;
25
-
26
- namespace Questions277480
27
- {
28
- public partial class MainWindow : Window, INotifyPropertyChanged
29
- {
30
- public string Text
31
- {
32
- get
33
- {
34
- return _Text;
35
- }
36
-
37
- set
38
- {
39
- if(value.StartsWith(_Text) || value.EndsWith(_Text))
40
- Set(ref _Text, value);
41
- }
42
- }
43
- private string _Text = "abcd"; // 初期値をカラにしたい場合 _Text = "";で (_Text;だとArgumentNullExceptionが出る)
44
-
45
- public MainWindow()
46
- {
47
- InitializeComponent();
48
- DataContext = this;
49
- }
50
-
51
- #region INotifyPropertyChanged
52
- public event PropertyChangedEventHandler PropertyChanged;
53
- protected bool Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
54
- {
55
- if(Equals(storage, value)) return false;
56
- storage = value;
57
- OnPropertyChanged(propertyName);
58
- return true;
59
- }
60
- protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
61
- {
62
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
63
- }
64
- #endregion
65
- }
66
- }
67
-
68
- ```
69
- 気になる点
70
- * `BackSpace`や`Enter`の動きがちょっと変
71
- * (`abcd`と表示されていたとする)aとbの間にカーソルがあったとき、aが入力できてしまう。bと入力するとカーソルがひとつ進む。
1
+ byoriさんの要件と厳密には合致していないかもしれませんが、ひとつ手軽にできる方法があります。
2
+
3
+ 1文字入力ごとに「新しい文字列が古い文字列と同じ文字列で始まっている」もしくは「新しい文字列が古い文字列と同じ文字列で終わっている」時だけ入力を受け付けるようにします。
4
+
5
+ ```xml
6
+ <Window
7
+ x:Class="Questions277480.MainWindow"
8
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
9
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
10
+ Width="800"
11
+ Height="450">
12
+ <Grid>
13
+ <TextBox
14
+ AcceptsReturn="True"
15
+ AcceptsTab="True"
16
+ Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}" />
17
+ </Grid>
18
+ </Window>
19
+ ```
20
+
21
+ ```cs
22
+ using System.ComponentModel;
23
+ using System.Runtime.CompilerServices;
24
+ using System.Windows;
25
+
26
+ namespace Questions277480
27
+ {
28
+ public partial class MainWindow : Window, INotifyPropertyChanged
29
+ {
30
+ public string Text
31
+ {
32
+ get
33
+ {
34
+ return _Text;
35
+ }
36
+
37
+ set
38
+ {
39
+ if(value.StartsWith(_Text) || value.EndsWith(_Text))
40
+ Set(ref _Text, value);
41
+ }
42
+ }
43
+ private string _Text = "abcd"; // 初期値をカラにしたい場合 _Text = "";で (_Text;だとArgumentNullExceptionが出る)
44
+
45
+ public MainWindow()
46
+ {
47
+ InitializeComponent();
48
+ DataContext = this;
49
+ }
50
+
51
+ #region INotifyPropertyChanged
52
+ public event PropertyChangedEventHandler PropertyChanged;
53
+ protected bool Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
54
+ {
55
+ if(Equals(storage, value)) return false;
56
+ storage = value;
57
+ OnPropertyChanged(propertyName);
58
+ return true;
59
+ }
60
+ protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
61
+ {
62
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
63
+ }
64
+ #endregion
65
+ }
66
+ }
67
+
68
+ ```
69
+ 気になる点
70
+ * `BackSpace`や`Enter`の動きがちょっと変
71
+ * (`abcd`と表示されていたとする)aとbの間にカーソルがあったとき、aが入力できてしまう。bと入力するとカーソルがひとつ進む。
72
72
  * `RichTextBox`ではこの手は使えない

1

ラムダ形式やめ

2020/07/15 02:51

投稿

TN8001
TN8001

スコア10104

answer CHANGED
@@ -29,7 +29,11 @@
29
29
  {
30
30
  public string Text
31
31
  {
32
+ get
33
+ {
32
- get => _Text;
34
+ return _Text;
35
+ }
36
+
33
37
  set
34
38
  {
35
39
  if(value.StartsWith(_Text) || value.EndsWith(_Text))
@@ -54,10 +58,13 @@
54
58
  return true;
55
59
  }
56
60
  protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
61
+ {
57
- => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
62
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
63
+ }
58
64
  #endregion
59
65
  }
60
66
  }
67
+
61
68
  ```
62
69
  気になる点
63
70
  * `BackSpace`や`Enter`の動きがちょっと変