teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

見直しキャンペーン中

2023/07/20 14:05

投稿

TN8001
TN8001

スコア10108

answer CHANGED
@@ -1,130 +1,102 @@
1
- `Binding="{Binding Name}"`で楽にバインディングできるのは、その`DataContext`が個々のItemになっているためです。
2
-
3
- `MainWindowViewModel`のものにバインディングしたい場合、`RelativeSource`等で探してくる必要があります(あまりに頻発するようですと、クラス設計が間違えている可能性があります)
4
-
5
- ```xaml
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
- Title="MainWindow"
12
- Width="800"
13
- Height="450">
14
- <Window.DataContext>
15
- <local:MainWindowViewModel />
16
- </Window.DataContext>
17
- <Grid>
18
- <Grid.RowDefinitions>
19
- <RowDefinition Height="Auto" />
20
- <RowDefinition />
21
- </Grid.RowDefinitions>
22
- <StackPanel Orientation="Horizontal">
23
- <Button Command="{Binding SetTrueCommand}" Content="IsEditMain True" />
24
- <Button Command="{Binding SetFalseCommand}" Content="IsEditMain False" />
25
- <Button Command="{Binding SelectedItem.SetTrueCommand, ElementName=dataGrid}" Content="Selected IsEditItem True" />
26
- <Button Command="{Binding SelectedItem.SetFalseCommand, ElementName=dataGrid}" Content="Selected IsEditItem False" />
27
-
28
- <TextBlock Text="{Binding IsEditMain, StringFormat=IsEditMain {0}}" />
29
- </StackPanel>
30
- <DataGrid
31
- x:Name="dataGrid"
32
- Grid.Row="1"
33
- AutoGenerateColumns="False"
34
- ItemsSource="{Binding Items}">
35
- <DataGrid.Columns>
36
- <DataGridTextColumn Binding="{Binding Name}" Header="名前" />
37
- <DataGridTextColumn Binding="{Binding Age}" Header="年齢">
38
- <DataGridTextColumn.CellStyle>
39
- <Style TargetType="{x:Type DataGridCell}">
40
- <Style.Triggers>
41
- <Trigger Property="IsSelected" Value="True">
42
- <Setter Property="Background" Value="#87CEEB" />
43
- <Setter Property="Foreground" Value="Black" />
44
- </Trigger>
45
- <DataTrigger Binding="{Binding IsEditItem}" Value="True">
46
- <Setter Property="BorderBrush" Value="Red" />
47
- </DataTrigger>
48
- <DataTrigger Binding="{Binding DataContext.IsEditMain, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Value="True">
49
- <Setter Property="Foreground" Value="Red" />
50
- </DataTrigger>
51
- </Style.Triggers>
52
- </Style>
53
- </DataGridTextColumn.CellStyle>
54
- </DataGridTextColumn>
55
- <DataGridTextColumn
56
- Binding="{Binding IsEditItem}"
57
- Header="IsEditItem"
58
- IsReadOnly="True" />
59
- </DataGrid.Columns>
60
- </DataGrid>
61
- </Grid>
62
- </Window>
63
- ```
64
-
65
- ```C#
66
- using System.Collections.ObjectModel;
67
- using System.Windows;
68
- using Livet;
69
- using Livet.Commands;
70
-
71
- namespace Questions241295
72
- {
73
- public partial class MainWindow : Window
74
- {
75
- public MainWindow() => InitializeComponent();
76
- }
77
-
78
- public class MainWindowViewModel : ViewModel
79
- {
80
- public ObservableCollection<Item> Items { get; }
81
-
82
- private bool isEdit;
83
- public bool IsEditMain { get => isEdit; set => RaisePropertyChangedIfSet(ref isEdit, value); }
84
-
85
- private ViewModelCommand setTrueCommand;
86
- public ViewModelCommand SetTrueCommand => setTrueCommand ?? (setTrueCommand = new ViewModelCommand(SetTrue));
87
-
88
- private ViewModelCommand setFalseCommand;
89
- public ViewModelCommand SetFalseCommand => setFalseCommand ?? (setFalseCommand = new ViewModelCommand(SetFalse));
90
-
91
- public MainWindowViewModel()
92
- {
93
- Items = new ObservableCollection<Item>
94
- {
95
- new Item("a", 24),
96
- new Item("b", 25)
97
- };
98
- }
99
-
100
- public void SetTrue() => IsEditMain = true;
101
- public void SetFalse() => IsEditMain = false;
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
+ ![アプリ画像](https://ddjkaamml8q8x.cloudfront.net/questions/2023-07-20/acffb10a-5c4a-440a-9941-0243e278201b.png)