回答編集履歴

2

見直しキャンペーン中

2023/08/10 09:05

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,6 +1,7 @@
1
1
  コードビハインドで書いたことがないので間違いがあるかもしれません。
2
2
 
3
- `myBinding.Source = stList; // ここが間違っていそう?`
3
+ > `myBinding.Source = stList; // ここが間違っていそう?`
4
+
4
5
  これ自体をなくせばいいんじゃないでしょうか。
5
6
 
6
7
  ---

1

見直しキャンペーン中

2023/07/18 22:07

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,183 +1,92 @@
1
1
  コードビハインドで書いたことがないので間違いがあるかもしれません。
2
2
 
3
-
4
-
5
3
  `myBinding.Source = stList; // ここが間違っていそう?`
6
-
7
4
  これ自体をなくせばいいんじゃないでしょうか。
8
-
9
-
10
5
 
11
6
  ---
12
7
 
13
-
14
-
15
8
  ここからは余計なお世話かもしれませんが、検証中にはまったのでついでに書いておきます。
16
9
 
17
-
18
-
19
10
  `CustomTextBox`ってことは編集するつもりなのかなと思い、`INotifyPropertyChanged`を実装して書き換えてみましたが値が反映されませんでした。
20
-
21
11
  `CellTemplate`と`CellEditingTemplate`があり`CellTemplate`のほうでは`TwoWay`にしようがダメみたいです(深堀はしていません)
22
12
 
23
-
24
-
25
- ```C#
13
+ ```cs
26
-
27
14
  using System.Collections.Generic;
28
-
29
15
  using System.ComponentModel;
30
-
31
16
  using System.Runtime.CompilerServices;
32
-
33
17
  using System.Windows;
34
-
35
18
  using System.Windows.Controls;
36
-
37
19
  using System.Windows.Data;
38
-
39
20
  using System.Windows.Media;
40
21
 
41
-
42
-
43
22
  namespace Questions240024
44
-
45
23
  {
46
-
47
24
  public partial class MainWindow : Window
48
-
49
25
  {
50
-
51
26
  public MainWindow()
52
-
53
27
  {
54
-
55
28
  //InitializeComponent();
56
29
 
57
-
58
-
59
30
  DataGrid dataGrid = new DataGrid();
60
-
61
31
  dataGrid.ItemsSource = new List<Row>
62
-
63
32
  {
64
-
65
33
  new Row{ Text = "AAA" },
66
-
67
34
  new Row{ Text = "BBB" },
68
-
69
35
  new Row{ Text = "CCC" },
70
-
71
36
  };
72
37
 
73
-
74
-
75
38
  DataGridTextColumn cm = new DataGridTextColumn
76
-
77
39
  {
78
-
79
40
  Header = "TextColumn",
80
-
81
41
  Binding = new Binding("Text")
82
-
83
42
  };
84
-
85
43
  dataGrid.Columns.Add(cm);
86
-
87
-
88
-
89
44
 
90
45
 
91
46
  DataGridTemplateColumn temp = new DataGridTemplateColumn { Header = "TemplateColumn" };
92
47
 
93
-
94
-
95
48
  FrameworkElementFactory cTextBlock = new FrameworkElementFactory(typeof(CustomTextBlock));
96
-
97
49
  cTextBlock.SetBinding(TextBlock.TextProperty, new Binding("Text"));
98
-
99
50
  temp.CellTemplate = new DataTemplate { VisualTree = cTextBlock };
100
51
 
101
-
102
-
103
52
  FrameworkElementFactory cTextBox = new FrameworkElementFactory(typeof(CustomTextBox));
104
-
105
53
  cTextBox.SetBinding(CustomTextBox.TextProperty, new Binding("Text"));
106
-
107
54
  temp.CellEditingTemplate = new DataTemplate { VisualTree = cTextBox };
108
-
109
-
110
55
 
111
56
  dataGrid.Columns.Add(temp);
112
57
 
113
58
 
114
-
115
-
116
-
117
59
  AddChild(dataGrid);
118
-
119
60
  }
120
-
121
61
  }
122
62
 
123
-
124
-
125
63
  internal class CustomTextBlock : TextBlock
126
-
127
64
  {
128
-
129
65
  public CustomTextBlock() : base() => Background = Brushes.LightPink;
130
-
131
66
  }
132
67
 
133
-
134
-
135
68
  internal class CustomTextBox : TextBox
136
-
137
69
  {
138
-
139
70
  public CustomTextBox() : base() => Background = Brushes.LightSkyBlue;
140
-
141
71
  }
142
72
 
143
-
144
-
145
73
  internal class Row : INotifyPropertyChanged
146
-
147
74
  {
148
-
149
75
  public string Text { get => _Text; set => SetProperty(ref _Text, value); }
150
-
151
76
  private string _Text;
152
77
 
153
-
154
-
155
78
  #region INotifyPropertyChanged
156
-
157
79
  public event PropertyChangedEventHandler PropertyChanged;
158
-
159
80
  protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName]string propertyName = null)
160
-
161
81
  {
162
-
163
82
  if(Equals(storage, value)) return false;
164
-
165
83
  storage = value;
166
-
167
84
  OnPropertyChanged(propertyName);
168
-
169
85
  return true;
170
-
171
86
  }
172
-
173
87
  protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
174
-
175
88
  #endregion
176
-
177
89
  }
178
-
179
90
  }
180
-
181
91
  ```
182
-
183
92
  ![イメージ説明](c46085ab72fa6fc50131e8317d1dfe00.gif)