質問編集履歴

7

ミニマムコードの追加

2019/03/06 02:47

投稿

BluOxy
BluOxy

スコア2663

test CHANGED
File without changes
test CHANGED
@@ -137,3 +137,95 @@
137
137
  `StringWrapper`を介さずに配列の要素を`Binding`できる方法はありませんでしょうか。
138
138
 
139
139
  何か思いつきがありましたら、是非ご教授をお願いいたします。
140
+
141
+
142
+
143
+ ### 2019/03/06 ミニマムコードを作りました
144
+
145
+
146
+
147
+ VS2017のWindowsフォームアプリから生成されたForm1.csに対して次のようなコードを記載しました。
148
+
149
+ 下記の書き方だとBindingされるのは配列の要素ではなく配列自体がBindingされてしまいます。
150
+
151
+
152
+
153
+ ```C#
154
+
155
+ using System;
156
+
157
+ using System.Collections.Generic;
158
+
159
+ using System.ComponentModel;
160
+
161
+ using System.Data;
162
+
163
+ using System.Drawing;
164
+
165
+ using System.Linq;
166
+
167
+ using System.Text;
168
+
169
+ using System.Threading.Tasks;
170
+
171
+ using System.Windows.Forms;
172
+
173
+
174
+
175
+ namespace BindingArray
176
+
177
+ {
178
+
179
+ public partial class Form1 : Form
180
+
181
+ {
182
+
183
+ private Hoge hoge = new Hoge();
184
+
185
+
186
+
187
+ public Form1()
188
+
189
+ {
190
+
191
+ InitializeComponent();
192
+
193
+
194
+
195
+ hoge.Str = "hoge";
196
+
197
+ hoge.Strs.Add("aaa");
198
+
199
+ var property = hoge.GetType().GetProperty("Strs");
200
+
201
+
202
+
203
+ var textBox = new TextBox();
204
+
205
+ //第3引数の文字列にproperty.Name+"[0]"とズルい書き方をしてもNGでした
206
+
207
+ textBox.DataBindings.Add(new Binding(nameof(textBox.Text), hoge, property.Name));
208
+
209
+ Controls.Add(textBox);
210
+
211
+ }
212
+
213
+ }
214
+
215
+ public class Hoge
216
+
217
+ {
218
+
219
+ public string Str { get; set; }
220
+
221
+
222
+
223
+ public BindingList<string> Strs { get; set; } = new BindingList<string>();
224
+
225
+ }
226
+
227
+ }
228
+
229
+
230
+
231
+ ```

6

bug fix

2019/03/06 02:47

投稿

BluOxy
BluOxy

スコア2663

test CHANGED
File without changes
test CHANGED
@@ -48,7 +48,7 @@
48
48
 
49
49
  var textBox = new TextBox();
50
50
 
51
- textBox.DataBindings.Add(new Binding(nameof(textBox.Text),hoge.Str,property.Name));
51
+ textBox.DataBindings.Add(new Binding(nameof(textBox.Text),hoge,property.Name));
52
52
 
53
53
  Controls.Add(textBox);
54
54
 
@@ -72,7 +72,7 @@
72
72
 
73
73
  var textBox = new TextBox();
74
74
 
75
- textBox.DataBindings.Add(new Binding(nameof(textBox.Text),hoge.Strs[3],property.Name));
75
+ textBox.DataBindings.Add(new Binding(nameof(textBox.Text),hoge,property.Name));
76
76
 
77
77
  Controls.Add(textBox);
78
78
 
@@ -120,7 +120,7 @@
120
120
 
121
121
  var textBox = new TextBox();
122
122
 
123
- textBox.DataBindings.Add(new Binding(nameof(textBox.Text),property.GetValue(hoge.Strs),property.Name));
123
+ textBox.DataBindings.Add(new Binding(nameof(textBox.Text),hoge,property.Name));
124
124
 
125
125
  Controls.Add(textBox);
126
126
 

5

code bug fix

2019/03/06 02:31

投稿

BluOxy
BluOxy

スコア2663

test CHANGED
File without changes
test CHANGED
@@ -48,7 +48,7 @@
48
48
 
49
49
  var textBox = new TextBox();
50
50
 
51
- textBox.DataBindings.Add(nameof(textBox.Text),hoge.Str,property.Name);
51
+ textBox.DataBindings.Add(new Binding(nameof(textBox.Text),hoge.Str,property.Name));
52
52
 
53
53
  Controls.Add(textBox);
54
54
 
@@ -72,7 +72,7 @@
72
72
 
73
73
  var textBox = new TextBox();
74
74
 
75
- textBox.DataBindings.Add(nameof(textBox.Text),hoge.Strs[3],property.Name);
75
+ textBox.DataBindings.Add(new Binding(nameof(textBox.Text),hoge.Strs[3],property.Name));
76
76
 
77
77
  Controls.Add(textBox);
78
78
 
@@ -120,7 +120,7 @@
120
120
 
121
121
  var textBox = new TextBox();
122
122
 
123
- textBox.DataBindings.Add(nameof(textBox.Text),property.GetValue(hoge.Strs),property.Name);
123
+ textBox.DataBindings.Add(new Binding(nameof(textBox.Text),property.GetValue(hoge.Strs),property.Name));
124
124
 
125
125
  Controls.Add(textBox);
126
126
 

4

code miss

2019/03/06 02:27

投稿

BluOxy
BluOxy

スコア2663

test CHANGED
File without changes
test CHANGED
@@ -24,9 +24,9 @@
24
24
 
25
25
  public class Hoge{
26
26
 
27
- string Str { get; set; }
27
+ public string Str { get; set; }
28
28
 
29
- string[] Strs { get; set; } = new string[8];
29
+ public string[] Strs { get; set; } = new string[8];
30
30
 
31
31
  }
32
32
 
@@ -42,7 +42,7 @@
42
42
 
43
43
  var hoge = new Hoge();
44
44
 
45
- var property = h.GetType().GetProperty("Str");
45
+ var property = hoge.GetType().GetProperty("Str");
46
46
 
47
47
 
48
48
 
@@ -66,7 +66,7 @@
66
66
 
67
67
  var hoge = new Hoge();
68
68
 
69
- var property = h.GetType().GetProperty("Strs");
69
+ var property = hoge.GetType().GetProperty("Strs");
70
70
 
71
71
 
72
72
 

3

閉じ括弧が抜けていた

2019/03/06 02:24

投稿

BluOxy
BluOxy

スコア2663

test CHANGED
File without changes
test CHANGED
@@ -48,7 +48,7 @@
48
48
 
49
49
  var textBox = new TextBox();
50
50
 
51
- textBox.DataBindings.Add(nameof(textBox.Text,hoge.Str,property.Name);
51
+ textBox.DataBindings.Add(nameof(textBox.Text),hoge.Str,property.Name);
52
52
 
53
53
  Controls.Add(textBox);
54
54
 
@@ -72,7 +72,7 @@
72
72
 
73
73
  var textBox = new TextBox();
74
74
 
75
- textBox.DataBindings.Add(nameof(textBox.Text,hoge.Strs[3],property.Name);
75
+ textBox.DataBindings.Add(nameof(textBox.Text),hoge.Strs[3],property.Name);
76
76
 
77
77
  Controls.Add(textBox);
78
78
 

2

言葉の修正

2019/03/05 08:26

投稿

BluOxy
BluOxy

スコア2663

test CHANGED
File without changes
test CHANGED
@@ -58,7 +58,7 @@
58
58
 
59
59
  `Strs`については以下のようなコードを書きました。
60
60
 
61
- 当然、`Binding`型(`string[]`)と`hoge.Strs[3]`の型(`string`)が違うと怒られ、うまく動作しません。
61
+ 当然、`property.Name`を示す型(`string[]`)と`hoge.Strs[3]`の型(`string`)が違うと怒られ、うまく動作しません。
62
62
 
63
63
 
64
64
 

1

コード例のメンバ名が小文字になっていたので修正

2019/03/05 08:23

投稿

BluOxy
BluOxy

スコア2663

test CHANGED
File without changes
test CHANGED
@@ -24,9 +24,9 @@
24
24
 
25
25
  public class Hoge{
26
26
 
27
- string str { get; set; }
27
+ string Str { get; set; }
28
28
 
29
- string[] strs { get; set; } = new string[8];
29
+ string[] Strs { get; set; } = new string[8];
30
30
 
31
31
  }
32
32
 
@@ -34,7 +34,7 @@
34
34
 
35
35
 
36
36
 
37
- `str`については次のような実装で`Binding`できます。
37
+ `Str`については次のような実装で`Binding`できます。
38
38
 
39
39
 
40
40
 
@@ -42,13 +42,13 @@
42
42
 
43
43
  var hoge = new Hoge();
44
44
 
45
- var property = h.GetType().GetProperty("str");
45
+ var property = h.GetType().GetProperty("Str");
46
46
 
47
47
 
48
48
 
49
49
  var textBox = new TextBox();
50
50
 
51
- textBox.DataBindings.Add(nameof(textBox.text,hoge.str,property.Name);
51
+ textBox.DataBindings.Add(nameof(textBox.Text,hoge.Str,property.Name);
52
52
 
53
53
  Controls.Add(textBox);
54
54
 
@@ -56,9 +56,9 @@
56
56
 
57
57
 
58
58
 
59
- `strs`については以下のようなコードを書きました。
59
+ `Strs`については以下のようなコードを書きました。
60
60
 
61
- 当然、`Binding`の型(`string[]`)と`hoge.strs[3]`の型(`string`)が違うと怒られ、うまく動作しません。
61
+ 当然、`Binding`の型(`string[]`)と`hoge.Strs[3]`の型(`string`)が違うと怒られ、うまく動作しません。
62
62
 
63
63
 
64
64
 
@@ -66,13 +66,13 @@
66
66
 
67
67
  var hoge = new Hoge();
68
68
 
69
- var property = h.GetType().GetProperty("strs");
69
+ var property = h.GetType().GetProperty("Strs");
70
70
 
71
71
 
72
72
 
73
73
  var textBox = new TextBox();
74
74
 
75
- textBox.DataBindings.Add(nameof(textBox.text,hoge.strs[3],property.Name);
75
+ textBox.DataBindings.Add(nameof(textBox.Text,hoge.Strs[3],property.Name);
76
76
 
77
77
  Controls.Add(textBox);
78
78
 
@@ -80,7 +80,7 @@
80
80
 
81
81
 
82
82
 
83
- 次のようなクラスを定義し、`strs`の型を`StringWrapper`型の配列に定義しなおしました。
83
+ 次のようなクラスを定義し、`Strs`の型を`StringWrapper`型の配列に定義しなおしました。
84
84
 
85
85
 
86
86
 
@@ -88,7 +88,7 @@
88
88
 
89
89
  public class StringWrapper{
90
90
 
91
- string str { get; set; }
91
+ string Str { get; set; }
92
92
 
93
93
  }
94
94
 
@@ -96,9 +96,9 @@
96
96
 
97
97
  public class Hoge{
98
98
 
99
- string str { get; set; }
99
+ string Str { get; set; }
100
100
 
101
- StringWrapper[] strs { get; set; } = new StringWrapper[8];
101
+ StringWrapper[] Strs { get; set; } = new StringWrapper[8];
102
102
 
103
103
  }
104
104
 
@@ -120,7 +120,7 @@
120
120
 
121
121
  var textBox = new TextBox();
122
122
 
123
- textBox.DataBindings.Add(nameof(textBox.text),property.GetValue(hoge.strs),property.Name);
123
+ textBox.DataBindings.Add(nameof(textBox.Text),property.GetValue(hoge.Strs),property.Name);
124
124
 
125
125
  Controls.Add(textBox);
126
126