質問編集履歴
1
問題の原因とどういう形で解決したいかを追記させていただきました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,20 +1,54 @@
|
|
1
|
-
DataGridTextColumnのDataGridCellを外部から文字列を書き換えよう
|
1
|
+
ApplicationのResourceにDataGridのStyleを定義して、DataGridTextColumnのDataGridCellを外部から文字列を書き換えられるようにできないでしょうか。
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
文字列を書き換えるために、DataGridCellのContentPresenter.Content.TextをソースとしたBindingを作成して添付プロパティにバインディングしています。
|
6
5
|
|
7
|
-
Cellを編集していない時はTextBlock、編集しているときはTextBoxをContentPresenter.ContentがもっているのでおなじPathでBindingできます。
|
8
6
|
|
7
|
+
文字列を書き換えるために、DataGridCellのContentPresenter.Content.Textをソースとした双方向(Mode=TwoWay)のBindingを作成して添付プロパティにバインディングしています。
|
8
|
+
|
9
|
+
DataGridCellは編集していない時はTextBlock、編集しているときはTextBoxをContentPresenter.ContentがもっているのでおなじPathでそれぞれのTextにBindingできます。
|
10
|
+
|
9
|
-
|
11
|
+
ソース上からBinding.Targetの添付プロパティを書き換えることで、Binding.SourceのTextBox・TextBlockのTextを操作しています。
|
10
12
|
|
11
13
|
|
12
14
|
|
15
|
+
Cellを編集しているときのTextBoxのTextを書き換えた時はDataGridCellの値も書き換わります。
|
16
|
+
|
13
|
-
編集していない時の
|
17
|
+
しかし、Cellを編集していない時のTextBlockのTextは書き換えてもCellの値には影響しません。
|
18
|
+
|
19
|
+
TextBlockが文字を表示するときに内部で使用するBinding.ModeがOneWayのため、Textを書き換えても影響しないようです。
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
このTextBlockのTextとDataGridCellを双方向にBindingできれば、解決するかと思っています。
|
24
|
+
|
25
|
+
すべてのDataGridに対して修正を行いため、ApplicationのResourceに修正のためのDataGridのデフォルトStyleを定義して対応したいと考えているのですが、対応方法が思い浮かびません。
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
試してはいませんが、列ごとのセルのスタイル「DataGridTextColumn.ElementStyle」をつかってContentTemplateを定義すれば双方向BindingのTextBlockを作成できるかもしれません。
|
30
|
+
|
31
|
+
ですがDataGridTextColumnは自分自身のStyleをもたないため、Resourceの中でデフォルトStyleを定義することができません。
|
32
|
+
|
33
|
+
だからDataGridTextColumn.ElementStyleを使った対応は断念しました。
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
ApplicationのResourceにDataGridのStyleを定義して、DataGridTextColumnのDataGridCellを外部から文字列を書き換えられるようにできないでしょうか。
|
38
|
+
|
39
|
+
私にはResourceにデフォルトのStyleを定義するぐらいしか思いつきませんが、他に解決方法があるのならそちらでもかまいません。
|
14
40
|
|
15
41
|
ご教示いただけると幸いです。
|
16
42
|
|
17
43
|
|
44
|
+
|
45
|
+
>データソースを変更して、表示の更新(同期)を行っても表示は変わりませんか?
|
46
|
+
|
47
|
+
たしかに各Cellにはデータソース(DataGridTextColumn.Binding)があるので、ソースを直接編集すれば対応は簡単です。
|
48
|
+
|
49
|
+
しかし、同様の処理を行いたい箇所は多義にわたるので局所的な対応はしたくありません。
|
50
|
+
|
51
|
+
汎用的な処理で吸収したいと考えております。
|
18
52
|
|
19
53
|
|
20
54
|
|
@@ -83,3 +117,47 @@
|
|
83
117
|
</Style>
|
84
118
|
|
85
119
|
```
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
-xaml.cs
|
124
|
+
|
125
|
+
```ここに言語を入力
|
126
|
+
|
127
|
+
public partial class LinkButtons : ItemsControl
|
128
|
+
|
129
|
+
{
|
130
|
+
|
131
|
+
public static readonly System.Windows.DependencyProperty TextProperty = DependencyProperty.Register(
|
132
|
+
|
133
|
+
nameof(Text), typeof(String), typeof(LinkButtons),new FrameworkPropertyMetadata("",FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)
|
134
|
+
|
135
|
+
);
|
136
|
+
|
137
|
+
/// <summary>
|
138
|
+
|
139
|
+
/// テキスト
|
140
|
+
|
141
|
+
/// </summary>
|
142
|
+
|
143
|
+
public String Text { get => (String)GetValue(TextProperty); set => SetValue(TextProperty, value); }
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
void Hoge()
|
148
|
+
|
149
|
+
{
|
150
|
+
|
151
|
+
Text =
|
152
|
+
|
153
|
+
"代入後、TextBox/TextBlockのTextも変わる。"+
|
154
|
+
|
155
|
+
"TextBoxの場合はBindingがTwoWayなのでCellの値も変わり、TextBlockの場合はOneWayなのでCellの値が変わらない";
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
```
|