###前提・実現したいこと
MVVMでバインドをしたいと考えています。
最終的な目標としてはジャンルリストでジャンルを選ぶと
ブックリストの本名の絞り込みが出来るといった実装にしたいと考えています。
ですが、入口のバインドの箇所がうまくいっていません。
バインドの仕組みについては
https://teratail.com/questions/45592
を参考にある程度は理解したつもりで、バインド自体も意図しない形でバインドできています。
次の一歩をどのようにしたら良いのかが分からない状況です。
###発生している問題・エラーメッセージ
現在下記のようなバインドがされています。
希望としてはtestにaddしているtest1とtest2のgenre(文学、helloWorld)が
出力されてほしいです。
しかし、実際にはtest1のgenreListのgenre内容が出力されています。
###xaml(View)の内容
cs
1<Window x:Class="WpfApp1.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApp1.viewModel" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="350" Width="525"> 9 10 <Window.DataContext> 11 <local:MainWindowViewModel /> 12 </Window.DataContext> 13 14 <Grid> 15 <ComboBox x:Name="comboBox" HorizontalAlignment="Left" Height="41" Margin="104,65,0,0" ItemsSource="{Binding GenreList.Genre}" VerticalAlignment="Top" Width="249"/> 16 <ComboBox x:Name="comboBox1" HorizontalAlignment="Left" Height="42" Margin="105,152,0,0" ItemsSource="{Binding BookList.BookName}" VerticalAlignment="Top" Width="248"/> 17 18 </Grid> 19</Window>
###xaml.cs(コードビハインド)
cs
1using System; 2using System.Collections.Generic; 3using System.Collections.ObjectModel; 4using System.Linq; 5using System.Text; 6using System.Threading.Tasks; 7 8namespace WpfApp1.viewModel { 9 class MainWindowViewModel { 10 11 //キーは本名、値はジャンル。 12 private GenreList genreList; 13 //キーは本名、値は作者 14 private BookList bookList; 15 16 public GenreList GenreList { get => genreList; set => genreList = value; } 17 public BookList BookList { get => bookList; set => bookList = value; } 18 19 public MainWindowViewModel() { 20 GenreList = new GenreList(); 21 BookList = new BookList(); 22 } 23 24 public static implicit operator ObservableCollection<object>(MainWindowViewModel v) { 25 throw new NotImplementedException(); 26 } 27 } 28 29 public class BookList { 30 private string bookName; 31 private string writer; 32 33 public string BookName { get => bookName; set => bookName = value; } 34 public string Writer { get => writer; set => writer = value; } 35 } 36 37 public class GenreList { 38 private string bookName; 39 private string genre; 40 41 public string BookName { get => bookName; set => bookName = value; } 42 public string Genre { get => genre; set => genre = value; } 43 } 44}
###MainWindow.xaml.cs(ビューモデル)
cs
1using System; 2using System.Collections.Generic; 3using System.Collections.ObjectModel; 4using System.Linq; 5using System.Text; 6using System.Threading.Tasks; 7using System.Windows; 8using System.Windows.Controls; 9using System.Windows.Data; 10using System.Windows.Documents; 11using System.Windows.Input; 12using System.Windows.Media; 13using System.Windows.Media.Imaging; 14using System.Windows.Navigation; 15using System.Windows.Shapes; 16using WpfApp1.viewModel; 17 18namespace WpfApp1 { 19 /// <summary> 20 /// MainWindow.xaml の相互作用ロジック 21 /// </summary> 22 public partial class MainWindow : Window { 23 public MainWindow() { 24 InitializeComponent(); 25 26 ObservableCollection<MainWindowViewModel> test = new ObservableCollection<MainWindowViewModel>(); 27 MainWindowViewModel test1 = new MainWindowViewModel(); 28 MainWindowViewModel test2 = new MainWindowViewModel(); 29 test1.BookList.BookName = "こころ"; 30 test1.BookList.Writer= "夏目漱石"; 31 test1.GenreList.Genre = "文学"; 32 test1.GenreList.BookName = "こころ"; 33 test2.BookList.BookName = "helloWorld"; 34 test2.BookList.Writer = "hogehoge"; 35 test2.GenreList.BookName="helloWorld"; 36 test2.GenreList.Genre = "hogehoge"; 37 38 test.Add(test1); 39 test.Add(test2); 40 DataContext = test; 41 } 42 } 43}
至らないところばかりかと思いますが、教えて頂ければ嬉しいです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/10/21 15:14 編集