質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

MVVM

MVVM(Model View ViewModel)は構築上のデザインパターンで、表現ロジック(ViewModel)によってデータ(Model)からページ(View)を分離させます。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

解決済

1回答

1797閲覧

WPFのバインドについて

退会済みユーザー

退会済みユーザー

総合スコア0

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

MVVM

MVVM(Model View ViewModel)は構築上のデザインパターンで、表現ロジック(ViewModel)によってデータ(Model)からページ(View)を分離させます。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

0グッド

1クリップ

投稿2017/10/21 09:20

###前提・実現したいこと
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}

至らないところばかりかと思いますが、教えて頂ければ嬉しいです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

現状は、文字列(配列)をバインドしているからそうなっているですね。

設計がおかしい。Listでもないものに、Listなんて言う名前は付けてはいけない。

複数のMainWindowViewModelを管理するVMが必要。

class Books :ObservableCollection<Book> {   public IEnumerable<string> GenreList => this.Select(n=>n.Genre).Distinct(); public IEnumerable<string> NameList => this.Select(n=>n.Name).Distinct(); public IEnumerable<string> WriterList => this.Select(n=>n.Writer).Distinct(); } class Book { public string Name {get; set;} public string writer {get; set;} public string Genre {get; set;} } public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Books books = new Books(); books.Add(new Book{Name = "こころ",Writer= "夏目漱石",Genre = "文学"}); books.Add(new Book{Name = "helloWorld",Writer= "hogehoge",Genre = "hogehoge"}); DataContext = books; } }

こんな感じ。
GenreListたちは、ObservableCollectionの変更に追従していないけど、INotifyPropertyChangedあたりをしらべて頑張ってみてください。
(VSで書いていないので細かいミスはあると思います)

投稿2017/10/21 13:16

編集2017/10/21 13:18
kiichi54321

総合スコア1984

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2017/10/21 15:14 編集

回答ありがとうございます。 ViewModel側を下記のようなソースにして、表示させることができました。 namespace WpfApp1.viewModel { class MainWindowViewModel : ObservableCollection<Book>{ public IEnumerable<string> GenreList => this.Select(n => n.Genre).Distinct(); public IEnumerable<string> NameList => this.Select(n => n.BookName).Distinct(); public IEnumerable<string> WriterList => this.Select(n => n.Writer).Distinct(); } public class Book{ private string bookName; private string genre; private string writer; public string BookName { get => bookName; set => bookName = value; } public string Writer { get => writer; set => writer = value; } public string Genre { get => genre; set => genre = value; } } } INotifyPropertyChangedについては別途勉強しようと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問