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

回答編集履歴

1

見直しキャンペーン中

2023/07/28 16:45

投稿

TN8001
TN8001

スコア10104

answer CHANGED
@@ -1,98 +1,98 @@
1
- > DicValueに変更後の"key"列が入ると思ったのですが設定されません。
2
-
3
- 「XAML バインド エラー」ウィンドウにエラーが出ています。
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')
11
- ```
12
-
13
- `key`が見つからないと言っています。そのため`DicValue`とバインドできていません。
14
- 指定するのは`Key`です。
15
-
16
-
17
- > 選択した結果をTwowayで(DicValue)反映させたいです。
18
-
19
- `TwoWay`の意味が初期値の反映とUIからの変更のみであれば、今のまま(自動プロパティ)でいいです。
20
- コードからも変更したい場合は、`PropertyChanged`を発砲する必要があります。
21
-
22
-
23
- ```xaml
24
- <Window
25
- x:Class="Questions355574.MainWindow"
26
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
27
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
28
- Width="800"
29
- Height="450">
30
- <StackPanel>
31
- <ComboBox
32
- DisplayMemberPath="Value"
33
- ItemsSource="{Binding ComboDic}"
34
- SelectedValue="{Binding DicValue}"
35
- SelectedValuePath="Key"
36
- SelectionChanged="cmb1_Changed" />
37
- <Button Click="Button_Click" Content="みかん" />
38
- </StackPanel>
39
- </Window>
40
- ```
41
-
42
- ```C#
43
- using System.Collections.Generic;
44
- using System.ComponentModel;
45
- using System.Diagnostics;
46
- using System.Runtime.CompilerServices;
47
- using System.Windows;
48
- using System.Windows.Controls;
49
-
50
- namespace Questions355574
51
- {
52
- public partial class MainWindow : Window, INotifyPropertyChanged
53
- {
54
- public Dictionary<string, string> ComboDic { get; }
55
-
56
- public string DicValue { get; set; }
57
- //public string DicValue { get => _DicValue; set => Set(ref _DicValue, value); }
58
- //private string _DicValue;
59
-
60
- public MainWindow()
61
- {
62
- InitializeComponent();
63
-
64
- ComboDic = new Dictionary<string, string>
65
- {
66
- { "10", "りんご" },
67
- { "20", "みかん" },
68
- { "30", "ぶどう" },
69
- };
70
- DicValue = "20";
71
-
72
- DataContext = this;
73
- }
74
-
75
- private void cmb1_Changed(object sender, SelectionChangedEventArgs e)
76
- {
77
- Debug.WriteLine(DicValue);
78
- }
79
-
80
- private void Button_Click(object sender, RoutedEventArgs e)
81
- {
82
- // DicValue { get; set; }のほうではこの変更が反映されない!
83
- // こういうことをしたい場合は、PropertyChangedを発砲する必要がある(コメントされているDicValueのほうを使う)
84
- DicValue = "20";
85
- }
86
-
87
-
88
- public event PropertyChangedEventHandler PropertyChanged;
89
- protected void Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
90
- {
91
- if (Equals(storage, value)) return;
92
- storage = value;
93
- OnPropertyChanged(propertyName);
94
- }
95
- protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
96
- }
97
- }
1
+ > DicValueに変更後の"key"列が入ると思ったのですが設定されません。
2
+
3
+ 「XAML バインド エラー」ウィンドウにエラーが出ています。
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')
11
+ ```
12
+
13
+ `key`が見つからないと言っています。そのため`DicValue`とバインドできていません。
14
+ 指定するのは`Key`です。
15
+
16
+
17
+ > 選択した結果をTwowayで(DicValue)反映させたいです。
18
+
19
+ `TwoWay`の意味が初期値の反映とUIからの変更のみであれば、今のまま(自動プロパティ)でいいです。
20
+ コードからも変更したい場合は、`PropertyChanged`を発砲する必要があります。
21
+
22
+
23
+ ```xml
24
+ <Window
25
+ x:Class="Questions355574.MainWindow"
26
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
27
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
28
+ Width="800"
29
+ Height="450">
30
+ <StackPanel>
31
+ <ComboBox
32
+ DisplayMemberPath="Value"
33
+ ItemsSource="{Binding ComboDic}"
34
+ SelectedValue="{Binding DicValue}"
35
+ SelectedValuePath="Key"
36
+ SelectionChanged="cmb1_Changed" />
37
+ <Button Click="Button_Click" Content="みかん" />
38
+ </StackPanel>
39
+ </Window>
40
+ ```
41
+
42
+ ```cs
43
+ using System.Collections.Generic;
44
+ using System.ComponentModel;
45
+ using System.Diagnostics;
46
+ using System.Runtime.CompilerServices;
47
+ using System.Windows;
48
+ using System.Windows.Controls;
49
+
50
+ namespace Questions355574
51
+ {
52
+ public partial class MainWindow : Window, INotifyPropertyChanged
53
+ {
54
+ public Dictionary<string, string> ComboDic { get; }
55
+
56
+ public string DicValue { get; set; }
57
+ //public string DicValue { get => _DicValue; set => Set(ref _DicValue, value); }
58
+ //private string _DicValue;
59
+
60
+ public MainWindow()
61
+ {
62
+ InitializeComponent();
63
+
64
+ ComboDic = new Dictionary<string, string>
65
+ {
66
+ { "10", "りんご" },
67
+ { "20", "みかん" },
68
+ { "30", "ぶどう" },
69
+ };
70
+ DicValue = "20";
71
+
72
+ DataContext = this;
73
+ }
74
+
75
+ private void cmb1_Changed(object sender, SelectionChangedEventArgs e)
76
+ {
77
+ Debug.WriteLine(DicValue);
78
+ }
79
+
80
+ private void Button_Click(object sender, RoutedEventArgs e)
81
+ {
82
+ // DicValue { get; set; }のほうではこの変更が反映されない!
83
+ // こういうことをしたい場合は、PropertyChangedを発砲する必要がある(コメントされているDicValueのほうを使う)
84
+ DicValue = "20";
85
+ }
86
+
87
+
88
+ public event PropertyChangedEventHandler PropertyChanged;
89
+ protected void Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
90
+ {
91
+ if (Equals(storage, value)) return;
92
+ storage = value;
93
+ OnPropertyChanged(propertyName);
94
+ }
95
+ protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
96
+ }
97
+ }
98
98
  ```