回答編集履歴
2
見直しキャンペーン中
test
CHANGED
@@ -1,143 +1,72 @@
|
|
1
1
|
byoriさんの要件と厳密には合致していないかもしれませんが、ひとつ手軽にできる方法があります。
|
2
|
-
|
3
|
-
|
4
2
|
|
5
3
|
1文字入力ごとに「新しい文字列が古い文字列と同じ文字列で始まっている」もしくは「新しい文字列が古い文字列と同じ文字列で終わっている」時だけ入力を受け付けるようにします。
|
6
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
|
+
```
|
7
20
|
|
21
|
+
```cs
|
22
|
+
using System.ComponentModel;
|
23
|
+
using System.Runtime.CompilerServices;
|
24
|
+
using System.Windows;
|
8
25
|
|
26
|
+
namespace Questions277480
|
27
|
+
{
|
28
|
+
public partial class MainWindow : Window, INotifyPropertyChanged
|
29
|
+
{
|
30
|
+
public string Text
|
31
|
+
{
|
32
|
+
get
|
33
|
+
{
|
9
|
-
|
34
|
+
return _Text;
|
35
|
+
}
|
10
36
|
|
37
|
+
set
|
38
|
+
{
|
39
|
+
if(value.StartsWith(_Text) || value.EndsWith(_Text))
|
11
|
-
|
40
|
+
Set(ref _Text, value);
|
41
|
+
}
|
42
|
+
}
|
43
|
+
private string _Text = "abcd"; // 初期値をカラにしたい場合 _Text = "";で (_Text;だとArgumentNullExceptionが出る)
|
12
44
|
|
13
|
-
|
45
|
+
public MainWindow()
|
46
|
+
{
|
47
|
+
InitializeComponent();
|
48
|
+
DataContext = this;
|
49
|
+
}
|
14
50
|
|
15
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
16
|
-
|
17
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
18
|
-
|
19
|
-
Width="800"
|
20
|
-
|
21
|
-
|
51
|
+
#region INotifyPropertyChanged
|
22
|
-
|
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);
|
23
|
-
|
58
|
+
return true;
|
24
|
-
|
25
|
-
|
59
|
+
}
|
26
|
-
|
27
|
-
|
60
|
+
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
28
|
-
|
29
|
-
|
61
|
+
{
|
30
|
-
|
31
|
-
|
62
|
+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
32
|
-
|
33
|
-
|
63
|
+
}
|
34
|
-
|
35
|
-
|
64
|
+
#endregion
|
65
|
+
}
|
66
|
+
}
|
36
67
|
|
37
68
|
```
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
```C#
|
42
|
-
|
43
|
-
using System.ComponentModel;
|
44
|
-
|
45
|
-
using System.Runtime.CompilerServices;
|
46
|
-
|
47
|
-
using System.Windows;
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
namespace Questions277480
|
52
|
-
|
53
|
-
{
|
54
|
-
|
55
|
-
public partial class MainWindow : Window, INotifyPropertyChanged
|
56
|
-
|
57
|
-
{
|
58
|
-
|
59
|
-
public string Text
|
60
|
-
|
61
|
-
{
|
62
|
-
|
63
|
-
get
|
64
|
-
|
65
|
-
{
|
66
|
-
|
67
|
-
return _Text;
|
68
|
-
|
69
|
-
}
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
set
|
74
|
-
|
75
|
-
{
|
76
|
-
|
77
|
-
if(value.StartsWith(_Text) || value.EndsWith(_Text))
|
78
|
-
|
79
|
-
Set(ref _Text, value);
|
80
|
-
|
81
|
-
}
|
82
|
-
|
83
|
-
}
|
84
|
-
|
85
|
-
private string _Text = "abcd"; // 初期値をカラにしたい場合 _Text = "";で (_Text;だとArgumentNullExceptionが出る)
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
public MainWindow()
|
90
|
-
|
91
|
-
{
|
92
|
-
|
93
|
-
InitializeComponent();
|
94
|
-
|
95
|
-
DataContext = this;
|
96
|
-
|
97
|
-
}
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
#region INotifyPropertyChanged
|
102
|
-
|
103
|
-
public event PropertyChangedEventHandler PropertyChanged;
|
104
|
-
|
105
|
-
protected bool Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
|
106
|
-
|
107
|
-
{
|
108
|
-
|
109
|
-
if(Equals(storage, value)) return false;
|
110
|
-
|
111
|
-
storage = value;
|
112
|
-
|
113
|
-
OnPropertyChanged(propertyName);
|
114
|
-
|
115
|
-
return true;
|
116
|
-
|
117
|
-
}
|
118
|
-
|
119
|
-
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
120
|
-
|
121
|
-
{
|
122
|
-
|
123
|
-
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
124
|
-
|
125
|
-
}
|
126
|
-
|
127
|
-
#endregion
|
128
|
-
|
129
|
-
}
|
130
|
-
|
131
|
-
}
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
```
|
136
|
-
|
137
69
|
気になる点
|
138
|
-
|
139
70
|
* `BackSpace`や`Enter`の動きがちょっと変
|
140
|
-
|
141
71
|
* (`abcd`と表示されていたとする)aとbの間にカーソルがあったとき、aが入力できてしまう。bと入力するとカーソルがひとつ進む。
|
142
|
-
|
143
72
|
* `RichTextBox`ではこの手は使えない
|
1
ラムダ形式やめ
test
CHANGED
@@ -60,7 +60,15 @@
|
|
60
60
|
|
61
61
|
{
|
62
62
|
|
63
|
+
get
|
64
|
+
|
65
|
+
{
|
66
|
+
|
63
|
-
|
67
|
+
return _Text;
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
|
64
72
|
|
65
73
|
set
|
66
74
|
|
@@ -110,13 +118,19 @@
|
|
110
118
|
|
111
119
|
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
112
120
|
|
121
|
+
{
|
122
|
+
|
113
|
-
|
123
|
+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
124
|
+
|
125
|
+
}
|
114
126
|
|
115
127
|
#endregion
|
116
128
|
|
117
129
|
}
|
118
130
|
|
119
131
|
}
|
132
|
+
|
133
|
+
|
120
134
|
|
121
135
|
```
|
122
136
|
|