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

回答編集履歴

2

見直しキャンペーン中

2023/07/29 13:03

投稿

TN8001
TN8001

スコア10180

answer CHANGED
@@ -5,6 +5,7 @@
5
5
  全部`(abab)`に変わってしまっては、訳が分からなくなると思うのですが(もちろん仮の話だということは分かっていますが、どういう意図かがさっぱりわかりません)
6
6
 
7
7
  状態がわかりやすいように`ListBox`にしていますが、`ComboBox`でも同じです。
8
+
8
9
  `INotifyPropertyChanged`に、↓を使いましたが何でもいいです。
9
10
  [NuGet Gallery | CommunityToolkit.Mvvm 7.1.2](https://www.nuget.org/packages/CommunityToolkit.Mvvm/7.1.2)
10
11
  ```xml

1

見直しキャンペーン中

2023/07/29 13:03

投稿

TN8001
TN8001

スコア10180

answer CHANGED
@@ -1,100 +1,100 @@
1
- > c#でコンボボックスの値が変更された際に値を変更したいです。
2
-
3
- 選択された`ComboList`の、`List1`を変えたいということでしょうか?
4
-
5
- 全部`(abab)`に変わってしまっては、訳が分からなくなると思うのですが(もちろん仮の話だということは分かっていますが、どういう意図かがさっぱりわかりません)
6
-
7
- 状態がわかりやすいように`ListBox`にしていますが、`ComboBox`でも同じです。
8
- `INotifyPropertyChanged`に、↓を使いましたが何でもいいです。
9
- [NuGet Gallery | CommunityToolkit.Mvvm 7.1.2](https://www.nuget.org/packages/CommunityToolkit.Mvvm/7.1.2)
10
- ```xaml
11
- <Window
12
- x:Class="Questions374830.MainWindow"
13
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
14
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
15
- xmlns:local="clr-namespace:Questions374830"
16
- Width="800"
17
- Height="450">
18
- <Window.DataContext>
19
- <local:ViewModel />
20
- </Window.DataContext>
21
- <StackPanel>
22
- <!--<ComboBox
23
- x:Name="CbxPileNo"
24
- DisplayMemberPath="List1"
25
- ItemsSource="{Binding ComboList}"
26
- SelectedValue="{Binding No, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
27
- SelectedValuePath="List1"
28
- SelectionChanged="CbxPileNo_SelectionChanged" />-->
29
-
30
- <ListBox
31
- x:Name="CbxPileNo"
32
- DisplayMemberPath="List1"
33
- ItemsSource="{Binding ComboList}"
34
- SelectedValue="{Binding No, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
35
- SelectedValuePath="List1"
36
- SelectionChanged="CbxPileNo_SelectionChanged" />
37
-
38
- <TextBlock Text="{Binding SelectedValue, ElementName=CbxPileNo, StringFormat=SelectedValue: {0}}" />
39
- <TextBlock Text="{Binding SelectedItem, ElementName=CbxPileNo, StringFormat=SelectedItem: {0}}" />
40
- <TextBlock Text="{Binding No, StringFormat=No: {0}}" />
41
- </StackPanel>
42
- </Window>
43
- ```
44
- ```C#
45
- using System.Collections.ObjectModel;
46
- using System.Windows;
47
- using System.Windows.Controls;
48
- using CommunityToolkit.Mvvm.ComponentModel;
49
-
50
- namespace Questions374830
51
- {
52
- public class ComboList : ObservableObject
53
- {
54
- public string List1 { get => _List1; set => SetProperty(ref _List1, value); }
55
- private string _List1;
56
-
57
- public override string ToString() => $"Item {{ List1 = {List1} }}";
58
- }
59
-
60
- public class ViewModel : ObservableObject
61
- {
62
- public ObservableCollection<ComboList> ComboList { get; } = new ObservableCollection<ComboList>();
63
-
64
- public string No { get => _No; set => SetProperty(ref _No, value); }
65
- private string _No;
66
-
67
-
68
- public ViewModel() => SetComboboxPileList();
69
-
70
- private void SetComboboxPileList()
71
- {
72
- ComboList.Add(new ComboList { List1 = "―", });
73
-
74
- for (var i = 0; i < 5; i++)
75
- {
76
- ComboList.Add(new ComboList { List1 = $"{i}", });
77
- }
78
- }
79
- }
80
-
81
- public partial class MainWindow : Window
82
- {
83
- public MainWindow() => InitializeComponent();
84
-
85
- private void CbxPileNo_SelectionChanged(object sender, SelectionChangedEventArgs e)
86
- {
87
- //CbxPileNo.SelectedValue = "(abab)";
88
- ((ComboList)CbxPileNo.SelectedItem).List1 = "(abab)";
89
- }
90
- }
91
- }
92
- ```
93
- `List1`を変更したときは`ListBox`の見た目は変わるが、`SelectedValue`やそのバインド先は変わらない(変えたのは`List1`であって、`ComboList`ではないため)
94
- 再選択したときには変わっている(うまく説明できないので動かしてみてください)
95
- ![アプリ画像](2758bbcd6c03895fe11ac72908ba24a8.png)
96
-
97
- ---
98
-
99
- `ComboList`のコレクションも`ComboList`は非常に違和感があります(`List<ComboItem> ComboList`とかなら納得できます)
100
- さらに中に`List1`がありますが、おそらく仮名でしょうから置いておきます(もし実際目にしたら「どんだけリストだよっ」と突っ込むと思いますw
1
+ > c#でコンボボックスの値が変更された際に値を変更したいです。
2
+
3
+ 選択された`ComboList`の、`List1`を変えたいということでしょうか?
4
+
5
+ 全部`(abab)`に変わってしまっては、訳が分からなくなると思うのですが(もちろん仮の話だということは分かっていますが、どういう意図かがさっぱりわかりません)
6
+
7
+ 状態がわかりやすいように`ListBox`にしていますが、`ComboBox`でも同じです。
8
+ `INotifyPropertyChanged`に、↓を使いましたが何でもいいです。
9
+ [NuGet Gallery | CommunityToolkit.Mvvm 7.1.2](https://www.nuget.org/packages/CommunityToolkit.Mvvm/7.1.2)
10
+ ```xml
11
+ <Window
12
+ x:Class="Questions374830.MainWindow"
13
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
14
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
15
+ xmlns:local="clr-namespace:Questions374830"
16
+ Width="800"
17
+ Height="450">
18
+ <Window.DataContext>
19
+ <local:ViewModel />
20
+ </Window.DataContext>
21
+ <StackPanel>
22
+ <!--<ComboBox
23
+ x:Name="CbxPileNo"
24
+ DisplayMemberPath="List1"
25
+ ItemsSource="{Binding ComboList}"
26
+ SelectedValue="{Binding No, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
27
+ SelectedValuePath="List1"
28
+ SelectionChanged="CbxPileNo_SelectionChanged" />-->
29
+
30
+ <ListBox
31
+ x:Name="CbxPileNo"
32
+ DisplayMemberPath="List1"
33
+ ItemsSource="{Binding ComboList}"
34
+ SelectedValue="{Binding No, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
35
+ SelectedValuePath="List1"
36
+ SelectionChanged="CbxPileNo_SelectionChanged" />
37
+
38
+ <TextBlock Text="{Binding SelectedValue, ElementName=CbxPileNo, StringFormat=SelectedValue: {0}}" />
39
+ <TextBlock Text="{Binding SelectedItem, ElementName=CbxPileNo, StringFormat=SelectedItem: {0}}" />
40
+ <TextBlock Text="{Binding No, StringFormat=No: {0}}" />
41
+ </StackPanel>
42
+ </Window>
43
+ ```
44
+ ```cs
45
+ using System.Collections.ObjectModel;
46
+ using System.Windows;
47
+ using System.Windows.Controls;
48
+ using CommunityToolkit.Mvvm.ComponentModel;
49
+
50
+ namespace Questions374830
51
+ {
52
+ public class ComboList : ObservableObject
53
+ {
54
+ public string List1 { get => _List1; set => SetProperty(ref _List1, value); }
55
+ private string _List1;
56
+
57
+ public override string ToString() => $"Item {{ List1 = {List1} }}";
58
+ }
59
+
60
+ public class ViewModel : ObservableObject
61
+ {
62
+ public ObservableCollection<ComboList> ComboList { get; } = new ObservableCollection<ComboList>();
63
+
64
+ public string No { get => _No; set => SetProperty(ref _No, value); }
65
+ private string _No;
66
+
67
+
68
+ public ViewModel() => SetComboboxPileList();
69
+
70
+ private void SetComboboxPileList()
71
+ {
72
+ ComboList.Add(new ComboList { List1 = "―", });
73
+
74
+ for (var i = 0; i < 5; i++)
75
+ {
76
+ ComboList.Add(new ComboList { List1 = $"{i}", });
77
+ }
78
+ }
79
+ }
80
+
81
+ public partial class MainWindow : Window
82
+ {
83
+ public MainWindow() => InitializeComponent();
84
+
85
+ private void CbxPileNo_SelectionChanged(object sender, SelectionChangedEventArgs e)
86
+ {
87
+ //CbxPileNo.SelectedValue = "(abab)";
88
+ ((ComboList)CbxPileNo.SelectedItem).List1 = "(abab)";
89
+ }
90
+ }
91
+ }
92
+ ```
93
+ `List1`を変更したときは`ListBox`の見た目は変わるが、`SelectedValue`やそのバインド先は変わらない(変えたのは`List1`であって、`ComboList`ではないため)
94
+ 再選択したときには変わっている(うまく説明できないので動かしてみてください)
95
+ ![アプリ画像](2758bbcd6c03895fe11ac72908ba24a8.png)
96
+
97
+ ---
98
+
99
+ `ComboList`のコレクションも`ComboList`は非常に違和感があります(`List<ComboItem> ComboList`とかなら納得できます)
100
+ さらに中に`List1`がありますが、おそらく仮名でしょうから置いておきます(もし実際目にしたら「どんだけリストだよっ」と突っ込むと思いますw