回答編集履歴
1
見直しキャンペーン中
answer
CHANGED
@@ -1,130 +1,102 @@
|
|
1
|
-
`Binding="{Binding Name}"`で楽にバインディングできるのは、その`DataContext`が個々のItemになっているためです。
|
2
|
-
|
3
|
-
`MainWindowViewModel`のものにバインディングしたい場合、`RelativeSource`等で探してくる必要があります(あまりに頻発するようですと、クラス設計が間違えている可能性があります)
|
4
|
-
|
5
|
-
```
|
6
|
-
<Window
|
7
|
-
x:Class="Questions241295.MainWindow"
|
8
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
9
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
10
|
-
xmlns:local="clr-namespace:Questions241295"
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
<
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
<Style
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
public
|
87
|
-
|
88
|
-
|
89
|
-
public
|
90
|
-
|
91
|
-
|
92
|
-
{
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
public class Item : ViewModel
|
105
|
-
{
|
106
|
-
private string name;
|
107
|
-
public string Name { get => name; set => RaisePropertyChangedIfSet(ref name, value); }
|
108
|
-
|
109
|
-
private int age;
|
110
|
-
public int Age { get => age; set => RaisePropertyChangedIfSet(ref age, value); }
|
111
|
-
|
112
|
-
private bool isEdit;
|
113
|
-
public bool IsEditItem { get => isEdit; set => RaisePropertyChangedIfSet(ref isEdit, value); }
|
114
|
-
|
115
|
-
private ViewModelCommand setTrueCommand;
|
116
|
-
public ViewModelCommand SetTrueCommand => setTrueCommand ?? (setTrueCommand = new ViewModelCommand(SetTrue));
|
117
|
-
|
118
|
-
private ViewModelCommand setFalseCommand;
|
119
|
-
public ViewModelCommand SetFalseCommand => setFalseCommand ?? (setFalseCommand = new ViewModelCommand(SetFalse));
|
120
|
-
|
121
|
-
public Item(string name, int age) => (Name, Age) = (name, age);
|
122
|
-
|
123
|
-
public void SetTrue() => IsEditItem = true;
|
124
|
-
public void SetFalse() => IsEditItem = false;
|
125
|
-
}
|
126
|
-
}
|
127
|
-
```
|
128
|
-
|
129
|
-
* `IsEditItem` 個々のIsEdit(ボーダー赤)
|
130
|
-
* `IsEditMain` 全体のIsEdit(文字赤)
|
1
|
+
`Binding="{Binding Name}"`で楽にバインディングできるのは、その`DataContext`が個々のItemになっているためです。
|
2
|
+
|
3
|
+
`MainWindowViewModel`のものにバインディングしたい場合、`RelativeSource`等で探してくる必要があります(あまりに頻発するようですと、クラス設計が間違えている可能性があります)
|
4
|
+
|
5
|
+
```xml
|
6
|
+
<Window
|
7
|
+
x:Class="Questions241295.MainWindow"
|
8
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
9
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
10
|
+
xmlns:local="clr-namespace:Questions241295"
|
11
|
+
Width="800"
|
12
|
+
Height="450">
|
13
|
+
<Window.DataContext>
|
14
|
+
<local:MainWindowViewModel />
|
15
|
+
</Window.DataContext>
|
16
|
+
<DockPanel>
|
17
|
+
<StackPanel DockPanel.Dock="Top">
|
18
|
+
<CheckBox VerticalContentAlignment="Center" Content="IsEditMain" IsChecked="{Binding IsEditMain}" />
|
19
|
+
</StackPanel>
|
20
|
+
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Items}">
|
21
|
+
<DataGrid.Columns>
|
22
|
+
<DataGridTextColumn Width="150" Binding="{Binding Name}" Header="名前" />
|
23
|
+
<DataGridTextColumn Width="100" Binding="{Binding Age}" Header="年齢">
|
24
|
+
<DataGridTextColumn.CellStyle>
|
25
|
+
<Style TargetType="{x:Type DataGridCell}">
|
26
|
+
<Setter Property="BorderThickness" Value="4" />
|
27
|
+
<Style.Triggers>
|
28
|
+
<Trigger Property="IsSelected" Value="True">
|
29
|
+
<Setter Property="Background" Value="#87CEEB" />
|
30
|
+
<Setter Property="Foreground" Value="Black" />
|
31
|
+
</Trigger>
|
32
|
+
<DataTrigger Binding="{Binding IsEditItem}" Value="True">
|
33
|
+
<Setter Property="BorderBrush" Value="Red" />
|
34
|
+
</DataTrigger>
|
35
|
+
<DataTrigger Binding="{Binding DataContext.IsEditMain, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Value="True">
|
36
|
+
<Setter Property="Foreground" Value="Red" />
|
37
|
+
</DataTrigger>
|
38
|
+
</Style.Triggers>
|
39
|
+
</Style>
|
40
|
+
</DataGridTextColumn.CellStyle>
|
41
|
+
</DataGridTextColumn>
|
42
|
+
<DataGridTemplateColumn Header="IsEditItem">
|
43
|
+
<DataGridTemplateColumn.CellTemplate>
|
44
|
+
<DataTemplate>
|
45
|
+
<CheckBox VerticalContentAlignment="Center" IsChecked="{Binding IsEditItem, UpdateSourceTrigger=PropertyChanged}" />
|
46
|
+
</DataTemplate>
|
47
|
+
</DataGridTemplateColumn.CellTemplate>
|
48
|
+
</DataGridTemplateColumn>
|
49
|
+
</DataGrid.Columns>
|
50
|
+
</DataGrid>
|
51
|
+
</DockPanel>
|
52
|
+
</Window>
|
53
|
+
```
|
54
|
+
|
55
|
+
```cs
|
56
|
+
using System.Collections.ObjectModel;
|
57
|
+
using System.Windows;
|
58
|
+
using Livet;
|
59
|
+
|
60
|
+
namespace Questions241295
|
61
|
+
{
|
62
|
+
public partial class MainWindow : Window
|
63
|
+
{
|
64
|
+
public MainWindow() => InitializeComponent();
|
65
|
+
}
|
66
|
+
|
67
|
+
public class MainWindowViewModel : ViewModel
|
68
|
+
{
|
69
|
+
public ObservableCollection<Item> Items { get; }
|
70
|
+
|
71
|
+
public bool IsEditMain { get => isEdit; set => RaisePropertyChangedIfSet(ref isEdit, value); }
|
72
|
+
private bool isEdit;
|
73
|
+
|
74
|
+
public MainWindowViewModel()
|
75
|
+
{
|
76
|
+
Items = new ObservableCollection<Item>
|
77
|
+
{
|
78
|
+
new Item("a", 24),
|
79
|
+
new Item("b", 25),
|
80
|
+
};
|
81
|
+
}
|
82
|
+
}
|
83
|
+
|
84
|
+
public class Item : ViewModel
|
85
|
+
{
|
86
|
+
public string Name { get => name; set => RaisePropertyChangedIfSet(ref name, value); }
|
87
|
+
private string name;
|
88
|
+
|
89
|
+
public int Age { get => age; set => RaisePropertyChangedIfSet(ref age, value); }
|
90
|
+
private int age;
|
91
|
+
|
92
|
+
public bool IsEditItem { get => isEdit; set => RaisePropertyChangedIfSet(ref isEdit, value); }
|
93
|
+
private bool isEdit;
|
94
|
+
|
95
|
+
public Item(string name, int age) => (Name, Age) = (name, age);
|
96
|
+
}
|
97
|
+
}
|
98
|
+
```
|
99
|
+
|
100
|
+
* `IsEditItem` 個々のIsEdit(ボーダー赤)
|
101
|
+
* `IsEditMain` 全体のIsEdit(文字赤)
|
102
|
+

|