発生している問題
WPFアプリケーションを作っておりMVVMで実装しようと考えています。
ListBoxのSelectionChagedイベントを実装しようとしていますが、想定通りの動きにならず困っています。
該当のソースコード
Viewのコードに一部コメント箇所があります。
別の質問で「ListBoxではSelectedItemの問題でできないため、CheckListBoxを使ってください」というご回答をいただき、
CheckListBoxを使った場合がコメントアウト箇所になります。
結果としてはどちらでも上手くいきませんでした。
C#
1//View 2 xmlns:xcad="http://schemas.xceed.com/wpf/xaml/toolkit" 3 mc:Ignorable="d" 4 Title="MainWindow" Height="350" Width="525"> 5 <Grid> 6 <ListBox HorizontalAlignment="Left" Height="56" Margin="64,67,0,0" VerticalAlignment="Top" Width="380" SelectedItem="{Binding SelectColorList}" ItemsSource="{Binding ColorList}"/> 7 <ListBox HorizontalAlignment="Left" Height="56" Margin="64,182,0,0" VerticalAlignment="Top" Width="380" ItemsSource="{Binding ItemList}"/> 8 9 <!--<xcad:CheckListBox HorizontalAlignment="Left" Height="56" Margin="64,67,0,0" VerticalAlignment="Top" Width="380" SelectedItemsOverride="{Binding SelectColorList}" ItemsSource="{Binding ColorList}"/> 10 <xcad:CheckListBox HorizontalAlignment="Left" Height="56" Margin="64,182,0,0" VerticalAlignment="Top" Width="380" ItemsSource="{Binding ItemList}"/>--> 11 12 <Label Content="色" HorizontalAlignment="Left" Height="24" Margin="65,38,0,0" VerticalAlignment="Top" Width="65"/> 13 <Label Content="アイテム" HorizontalAlignment="Left" Height="24" Margin="64,153,0,0" VerticalAlignment="Top" Width="65"/> 14 15 <Button Content="Button" HorizontalAlignment="Left" Height="37" Margin="354,260,0,0" VerticalAlignment="Top" Width="90" Click="Button_Click"/> 16 </Grid>
C#
1//ViewModel 2 public class MainViewModel : INotifyPropertyChanged 3 { 4 private ObservableCollection<string> ColorListVal; 5 public ObservableCollection<string> ColorList 6 { 7 get { return ColorListVal; } 8 set 9 { 10 ColorListVal = value; 11 NotifyPropertyChanged("ColorList"); 12 13 } 14 } 15 16 private ObservableCollection<string> SelectColorListVal = new ObservableCollection<string>(); 17 public ObservableCollection<string> SelectColorList 18 { 19 get { return SelectColorListVal; } 20 set 21 { 22 SelectColorListVal = value; 23 NotifyPropertyChanged("SelectColorList"); 24 25 ItemListVal.Clear(); 26 27 for (int i = 0; i < SelectColorList.Count; i++) 28 { 29 switch (SelectColorList[i]) 30 { 31 case "赤": 32 ItemListVal.Add("りんご"); 33 ItemListVal.Add("太陽"); 34 ItemListVal.Add("チューリップ"); 35 break; 36 37 case "青": 38 ItemListVal.Add("海"); 39 ItemListVal.Add("ブルーハワイ"); 40 ItemListVal.Add("空"); 41 break; 42 43 case "黄": 44 ItemListVal.Add("レモン"); 45 ItemListVal.Add("パイナップル"); 46 ItemListVal.Add("チューリップ"); 47 break; 48 49 case "緑": 50 ItemListVal.Add("青りんご"); 51 ItemListVal.Add("野菜"); 52 ItemListVal.Add("お茶"); 53 break; 54 } 55 } 56 } 57 } 58 59 private ObservableCollection<string> ItemListVal; 60 public ObservableCollection<string> ItemList 61 { 62 get { return ItemListVal; } 63 set 64 { 65 ItemListVal = value; 66 NotifyPropertyChanged("ItemList"); 67 } 68 } 69 70 71 72 public MainViewModel() 73 { 74 ColorListVal = new ObservableCollection<string>(){ "赤", "青", "黄", "緑" }; 75 ItemListVal = new ObservableCollection<string>() { "何も選択されていません"}; 76 } 77 78 79 80 public event PropertyChangedEventHandler PropertyChanged; 81 private void NotifyPropertyChanged(String info) 82 { 83 if (PropertyChanged != null) 84 { 85 PropertyChanged(this, new PropertyChangedEventArgs(info)); 86 } 87 } 88 }
C#
1//コードビハインド 2 public partial class MainWindow : Window 3 { 4 private MainViewModel viewmodel; 5 public MainWindow() 6 { 7 InitializeComponent(); 8 viewmodel = new MainViewModel(); 9 this.DataContext = viewmodel; 10 } 11 12 private void Button_Click(object sender, RoutedEventArgs e) 13 { 14 string s1 = ""; 15 16 for (int i = 0; i < viewmodel.SelectColorList.Count; i++) 17 { 18 s1 += viewmodel.SelectColorList[i] + "\r\n"; 19 } 20 21 MessageBox.Show(s1); 22 } 23 }
試したこと
本来であれば「色」リストの項目を選択すると「アイテム」リストに対応するアイテムが反映されるのですが、
初期値である「何も選択されていません」から変わりません。
プロパティのデータバインドが上手くいっていないのかと思い、
ボタンを追加して現在選択されている「色」リストの項目を確認するようにもしましたが、そこは正常に反映されています。
やはりプロパティのセッターを使うのではなく、コマンドをバインドさせる形になるのでしょうか?
ご助言いただけますと幸いです。
###補足
ちなみに本番アプリケーションでは複数選択をするので SelectionMode="Extended" プロパティを設定します。(ListBoxの場合)
ListBox、ChechListBoxどちらを使うかは大きな問題ではないのですが、
複数選択した時にも「アイテム」リストに複数の色アイテムが表示されるようにできればと思っています。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/18 06:57
2019/01/18 07:16 編集
2019/01/18 08:03