回答編集履歴
1
見直しキャンペーン中
test
CHANGED
@@ -1,259 +1,102 @@
|
|
1
1
|
`Binding="{Binding Name}"`で楽にバインディングできるのは、その`DataContext`が個々のItemになっているためです。
|
2
|
-
|
3
|
-
|
4
2
|
|
5
3
|
`MainWindowViewModel`のものにバインディングしたい場合、`RelativeSource`等で探してくる必要があります(あまりに頻発するようですと、クラス設計が間違えている可能性があります)
|
6
4
|
|
7
|
-
|
8
|
-
|
9
|
-
```x
|
5
|
+
```xml
|
10
|
-
|
11
6
|
<Window
|
12
|
-
|
13
7
|
x:Class="Questions241295.MainWindow"
|
14
|
-
|
15
8
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
16
|
-
|
17
9
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
18
|
-
|
19
10
|
xmlns:local="clr-namespace:Questions241295"
|
20
|
-
|
21
|
-
Title="MainWindow"
|
22
|
-
|
23
11
|
Width="800"
|
24
|
-
|
25
12
|
Height="450">
|
26
|
-
|
27
13
|
<Window.DataContext>
|
28
|
-
|
29
14
|
<local:MainWindowViewModel />
|
30
|
-
|
31
15
|
</Window.DataContext>
|
32
|
-
|
33
|
-
<
|
16
|
+
<DockPanel>
|
34
|
-
|
35
|
-
<Grid.RowDefinitions>
|
36
|
-
|
37
|
-
<RowDefinition Height="Auto" />
|
38
|
-
|
39
|
-
<RowDefinition />
|
40
|
-
|
41
|
-
</Grid.RowDefinitions>
|
42
|
-
|
43
|
-
<StackPanel
|
17
|
+
<StackPanel DockPanel.Dock="Top">
|
44
|
-
|
45
|
-
<Button Command="{Binding SetTrueCommand}" Content="IsEditMain True" />
|
46
|
-
|
47
|
-
<Button Command="{Binding SetFalseCommand}" Content="IsEditMain False" />
|
48
|
-
|
49
|
-
<B
|
18
|
+
<CheckBox VerticalContentAlignment="Center" Content="IsEditMain" IsChecked="{Binding IsEditMain}" />
|
50
|
-
|
51
|
-
<Button Command="{Binding SelectedItem.SetFalseCommand, ElementName=dataGrid}" Content="Selected IsEditItem False" />
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
<TextBlock Text="{Binding IsEditMain, StringFormat=IsEditMain {0}}" />
|
56
|
-
|
57
19
|
</StackPanel>
|
58
|
-
|
59
|
-
<DataGrid
|
60
|
-
|
61
|
-
x:Name="dataGrid"
|
62
|
-
|
63
|
-
Grid.Row="1"
|
64
|
-
|
65
|
-
AutoGenerateColumns="False"
|
66
|
-
|
67
|
-
ItemsSource="{Binding Items}">
|
20
|
+
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Items}">
|
68
|
-
|
69
21
|
<DataGrid.Columns>
|
70
|
-
|
71
|
-
<DataGridTextColumn Binding="{Binding Name}" Header="名前" />
|
22
|
+
<DataGridTextColumn Width="150" Binding="{Binding Name}" Header="名前" />
|
72
|
-
|
73
|
-
<DataGridTextColumn Binding="{Binding Age}" Header="年齢">
|
23
|
+
<DataGridTextColumn Width="100" Binding="{Binding Age}" Header="年齢">
|
74
|
-
|
75
24
|
<DataGridTextColumn.CellStyle>
|
76
|
-
|
77
25
|
<Style TargetType="{x:Type DataGridCell}">
|
78
|
-
|
26
|
+
<Setter Property="BorderThickness" Value="4" />
|
79
27
|
<Style.Triggers>
|
80
|
-
|
81
28
|
<Trigger Property="IsSelected" Value="True">
|
82
|
-
|
83
29
|
<Setter Property="Background" Value="#87CEEB" />
|
84
|
-
|
85
30
|
<Setter Property="Foreground" Value="Black" />
|
86
|
-
|
87
31
|
</Trigger>
|
88
|
-
|
89
32
|
<DataTrigger Binding="{Binding IsEditItem}" Value="True">
|
90
|
-
|
91
33
|
<Setter Property="BorderBrush" Value="Red" />
|
92
|
-
|
93
34
|
</DataTrigger>
|
94
|
-
|
95
35
|
<DataTrigger Binding="{Binding DataContext.IsEditMain, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Value="True">
|
96
|
-
|
97
36
|
<Setter Property="Foreground" Value="Red" />
|
98
|
-
|
99
37
|
</DataTrigger>
|
100
|
-
|
101
38
|
</Style.Triggers>
|
102
|
-
|
103
39
|
</Style>
|
104
|
-
|
105
40
|
</DataGridTextColumn.CellStyle>
|
106
|
-
|
107
41
|
</DataGridTextColumn>
|
108
|
-
|
42
|
+
<DataGridTemplateColumn Header="IsEditItem">
|
43
|
+
<DataGridTemplateColumn.CellTemplate>
|
44
|
+
<DataTemplate>
|
45
|
+
<CheckBox VerticalContentAlignment="Center" IsChecked="{Binding IsEditItem, UpdateSourceTrigger=PropertyChanged}" />
|
46
|
+
</DataTemplate>
|
47
|
+
</DataGridTemplateColumn.CellTemplate>
|
109
|
-
<DataGridTe
|
48
|
+
</DataGridTemplateColumn>
|
110
|
-
|
111
|
-
Binding="{Binding IsEditItem}"
|
112
|
-
|
113
|
-
Header="IsEditItem"
|
114
|
-
|
115
|
-
IsReadOnly="True" />
|
116
|
-
|
117
49
|
</DataGrid.Columns>
|
118
|
-
|
119
50
|
</DataGrid>
|
120
|
-
|
121
|
-
</
|
51
|
+
</DockPanel>
|
122
|
-
|
123
52
|
</Window>
|
124
|
-
|
125
53
|
```
|
126
54
|
|
127
|
-
|
128
|
-
|
129
|
-
```
|
55
|
+
```cs
|
130
|
-
|
131
56
|
using System.Collections.ObjectModel;
|
132
|
-
|
133
57
|
using System.Windows;
|
134
|
-
|
135
58
|
using Livet;
|
136
59
|
|
137
|
-
using Livet.Commands;
|
138
|
-
|
139
|
-
|
140
|
-
|
141
60
|
namespace Questions241295
|
142
|
-
|
143
61
|
{
|
144
|
-
|
145
62
|
public partial class MainWindow : Window
|
146
|
-
|
147
63
|
{
|
148
|
-
|
149
64
|
public MainWindow() => InitializeComponent();
|
150
|
-
|
151
65
|
}
|
152
66
|
|
153
|
-
|
154
|
-
|
155
67
|
public class MainWindowViewModel : ViewModel
|
156
|
-
|
157
68
|
{
|
158
|
-
|
159
69
|
public ObservableCollection<Item> Items { get; }
|
160
70
|
|
161
|
-
|
71
|
+
public bool IsEditMain { get => isEdit; set => RaisePropertyChangedIfSet(ref isEdit, value); }
|
162
|
-
|
163
72
|
private bool isEdit;
|
164
73
|
|
165
|
-
public bool IsEditMain { get => isEdit; set => RaisePropertyChangedIfSet(ref isEdit, value); }
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
private ViewModelCommand setTrueCommand;
|
170
|
-
|
171
|
-
public ViewModelCommand SetTrueCommand => setTrueCommand ?? (setTrueCommand = new ViewModelCommand(SetTrue));
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
private ViewModelCommand setFalseCommand;
|
176
|
-
|
177
|
-
public ViewModelCommand SetFalseCommand => setFalseCommand ?? (setFalseCommand = new ViewModelCommand(SetFalse));
|
178
|
-
|
179
|
-
|
180
|
-
|
181
74
|
public MainWindowViewModel()
|
182
|
-
|
183
75
|
{
|
184
|
-
|
185
76
|
Items = new ObservableCollection<Item>
|
186
|
-
|
187
77
|
{
|
188
|
-
|
189
78
|
new Item("a", 24),
|
190
|
-
|
191
|
-
new Item("b", 25)
|
79
|
+
new Item("b", 25),
|
192
|
-
|
193
80
|
};
|
194
|
-
|
195
81
|
}
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
public void SetTrue() => IsEditMain = true;
|
200
|
-
|
201
|
-
public void SetFalse() => IsEditMain = false;
|
202
|
-
|
203
82
|
}
|
204
83
|
|
205
|
-
|
206
|
-
|
207
84
|
public class Item : ViewModel
|
208
|
-
|
209
85
|
{
|
210
|
-
|
86
|
+
public string Name { get => name; set => RaisePropertyChangedIfSet(ref name, value); }
|
211
87
|
private string name;
|
212
88
|
|
213
|
-
public
|
89
|
+
public int Age { get => age; set => RaisePropertyChangedIfSet(ref age, value); }
|
214
|
-
|
215
|
-
|
216
|
-
|
217
90
|
private int age;
|
218
91
|
|
219
|
-
public i
|
92
|
+
public bool IsEditItem { get => isEdit; set => RaisePropertyChangedIfSet(ref isEdit, value); }
|
220
|
-
|
221
|
-
|
222
|
-
|
223
93
|
private bool isEdit;
|
224
94
|
|
225
|
-
public bool IsEditItem { get => isEdit; set => RaisePropertyChangedIfSet(ref isEdit, value); }
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
private ViewModelCommand setTrueCommand;
|
230
|
-
|
231
|
-
public ViewModelCommand SetTrueCommand => setTrueCommand ?? (setTrueCommand = new ViewModelCommand(SetTrue));
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
private ViewModelCommand setFalseCommand;
|
236
|
-
|
237
|
-
public ViewModelCommand SetFalseCommand => setFalseCommand ?? (setFalseCommand = new ViewModelCommand(SetFalse));
|
238
|
-
|
239
|
-
|
240
|
-
|
241
95
|
public Item(string name, int age) => (Name, Age) = (name, age);
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
public void SetTrue() => IsEditItem = true;
|
246
|
-
|
247
|
-
public void SetFalse() => IsEditItem = false;
|
248
|
-
|
249
96
|
}
|
250
|
-
|
251
97
|
}
|
252
|
-
|
253
98
|
```
|
254
99
|
|
255
|
-
|
256
|
-
|
257
100
|
* `IsEditItem` 個々のIsEdit(ボーダー赤)
|
258
|
-
|
259
101
|
* `IsEditMain` 全体のIsEdit(文字赤)
|
102
|
+
![アプリ画像](https://ddjkaamml8q8x.cloudfront.net/questions/2023-07-20/acffb10a-5c4a-440a-9941-0243e278201b.png)
|