回答編集履歴

1

見直しキャンペーン中

2023/07/28 16:45

投稿

TN8001
TN8001

スコア9341

test CHANGED
@@ -1,195 +1,98 @@
1
1
  > DicValueに変更後の"key"列が入ると思ったのですが設定されません。
2
2
 
3
-
4
-
5
3
  「XAML バインド エラー」ウィンドウにエラーが出ています。
6
-
4
+ ```
5
+ 重大度レベル データ コンテキスト バインド パス ターゲット ターゲット型 説明 ファイル 行 プロジェクト
6
+ エラー KeyValuePair`2 key ComboBox.NoTarget Object 型 KeyValuePair`2 のオブジェクトに key プロパティが見つかりません。 \mainwindow.xaml 13 Questions355574
7
+ ```
8
+ 「XAML バインド エラー」ウィンドウが見つからなければ、「出力」ウィンドウでもいいです。
9
+ ```
10
+ System.Windows.Data Error: 40 : BindingExpression path error: 'key' property not found on 'object' ''KeyValuePair`2' (HashCode=54645477)'. BindingExpression:Path=key; DataItem='KeyValuePair`2' (HashCode=54645477); target element is 'ComboBox' (Name=''); target property is 'NoTarget' (type 'Object')
7
11
  ```
8
12
 
9
- 重大度レベル データ コンテキスト バインド パス ターゲット ターゲット型 説明 ファイル 行 プロジェクト
10
-
11
- エラー KeyValuePair`2 key ComboBox.NoTarget Object 型 KeyValuePair`2 のオブジェクトに key プロパティが見つかりません。 \mainwindow.xaml 13 Questions355574
12
-
13
- ```
14
-
15
- 「XAML バインド エラー」ウィンドウが見つからなければ、「出力」ウィンドウでもいいです。
16
-
17
- ```
18
-
19
- System.Windows.Data Error: 40 : BindingExpression path error: 'key' property not found on 'object' ''KeyValuePair`2' (HashCode=54645477)'. BindingExpression:Path=key; DataItem='KeyValuePair`2' (HashCode=54645477); target element is 'ComboBox' (Name=''); target property is 'NoTarget' (type 'Object')
20
-
21
- ```
22
-
23
-
24
-
25
13
  `key`が見つからないと言っています。そのため`DicValue`とバインドできていません。
26
-
27
14
  指定するのは`Key`です。
28
-
29
-
30
-
31
15
 
32
16
 
33
17
  > 選択した結果をTwowayで(DicValue)反映させたいです。
34
18
 
35
-
36
-
37
19
  `TwoWay`の意味が初期値の反映とUIからの変更のみであれば、今のまま(自動プロパティ)でいいです。
38
-
39
20
  コードからも変更したい場合は、`PropertyChanged`を発砲する必要があります。
40
21
 
41
22
 
42
-
43
-
44
-
45
- ```xaml
23
+ ```xml
46
-
47
24
  <Window
48
-
49
25
  x:Class="Questions355574.MainWindow"
50
-
51
26
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
52
-
53
27
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
54
-
55
28
  Width="800"
56
-
57
29
  Height="450">
58
-
59
30
  <StackPanel>
60
-
61
31
  <ComboBox
62
-
63
32
  DisplayMemberPath="Value"
64
-
65
33
  ItemsSource="{Binding ComboDic}"
66
-
67
34
  SelectedValue="{Binding DicValue}"
68
-
69
35
  SelectedValuePath="Key"
70
-
71
36
  SelectionChanged="cmb1_Changed" />
72
-
73
37
  <Button Click="Button_Click" Content="みかん" />
74
-
75
38
  </StackPanel>
76
-
77
39
  </Window>
78
-
79
40
  ```
80
41
 
81
-
82
-
83
- ```C#
42
+ ```cs
84
-
85
43
  using System.Collections.Generic;
86
-
87
44
  using System.ComponentModel;
88
-
89
45
  using System.Diagnostics;
90
-
91
46
  using System.Runtime.CompilerServices;
92
-
93
47
  using System.Windows;
94
-
95
48
  using System.Windows.Controls;
96
49
 
97
-
98
-
99
50
  namespace Questions355574
100
-
101
51
  {
102
-
103
52
  public partial class MainWindow : Window, INotifyPropertyChanged
104
-
105
53
  {
106
-
107
54
  public Dictionary<string, string> ComboDic { get; }
108
55
 
109
-
110
-
111
56
  public string DicValue { get; set; }
112
-
113
57
  //public string DicValue { get => _DicValue; set => Set(ref _DicValue, value); }
114
-
115
58
  //private string _DicValue;
116
59
 
117
-
118
-
119
60
  public MainWindow()
120
-
121
61
  {
122
-
123
62
  InitializeComponent();
124
63
 
125
-
126
-
127
64
  ComboDic = new Dictionary<string, string>
128
-
129
65
  {
130
-
131
66
  { "10", "りんご" },
132
-
133
67
  { "20", "みかん" },
134
-
135
68
  { "30", "ぶどう" },
136
-
137
69
  };
138
-
139
70
  DicValue = "20";
140
71
 
72
+ DataContext = this;
73
+ }
141
74
 
75
+ private void cmb1_Changed(object sender, SelectionChangedEventArgs e)
76
+ {
77
+ Debug.WriteLine(DicValue);
78
+ }
142
79
 
80
+ private void Button_Click(object sender, RoutedEventArgs e)
81
+ {
82
+ // DicValue { get; set; }のほうではこの変更が反映されない!
83
+ // こういうことをしたい場合は、PropertyChangedを発砲する必要がある(コメントされているDicValueのほうを使う)
143
- DataContext = this;
84
+ DicValue = "20";
144
-
145
85
  }
146
86
 
147
87
 
148
-
149
- private void cmb1_Changed(object sender, SelectionChangedEventArgs e)
88
+ public event PropertyChangedEventHandler PropertyChanged;
150
-
89
+ protected void Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
151
90
  {
152
-
91
+ if (Equals(storage, value)) return;
92
+ storage = value;
153
- Debug.WriteLine(DicValue);
93
+ OnPropertyChanged(propertyName);
154
-
155
94
  }
156
-
157
-
158
-
159
- private void Button_Click(object sender, RoutedEventArgs e)
160
-
161
- {
162
-
163
- // DicValue { get; set; }のほうではこの変更が反映されない!
164
-
165
- // こういうことをしたい場合は、PropertyChangedを発砲する必要がある(コメントされているDicValueのほうを使う)
166
-
167
- DicValue = "20";
168
-
169
- }
170
-
171
-
172
-
173
-
174
-
175
- public event PropertyChangedEventHandler PropertyChanged;
176
-
177
- protected void Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
178
-
179
- {
180
-
181
- if (Equals(storage, value)) return;
182
-
183
- storage = value;
184
-
185
- OnPropertyChanged(propertyName);
186
-
187
- }
188
-
189
95
  protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
190
-
191
96
  }
192
-
193
97
  }
194
-
195
98
  ```