2016-05-06 7 views
0

私のMainViewにはMainViewModelというViewModelがあります。 MainViewModelは、ダイアログを作成し、私はダイアログが私は私がからコレクションにバインドListBoxを持つダイアログでDisplayMemberPathが表示されないプロパティ - 'ViewModel'にプロパティが見つかりません

var MyInnerDlg = new MyInnerDlgView(); 
MyInnerDlg.DataContext = this; 
MyInnerDlg.ShowDialog(); 

を行うMainViewModelからダイアログを作成するには、既存のMainViewModel

するViewModelにしたいですダイアログ

の私の MainViewModel

public ObservableCollection<MyItemViewModel> MyList { get; set; } 

XAML

<Border Margin="5" BorderThickness="2"> 
    <ListBox ItemsSource="{Binding MyList}" DisplayMemberPath="{Binding MyList.Name}" /> 
</Border> 

NameプロパティはMyItemViewModelです。上記のコードで私はエラーが表示されます:

Name property not found in MainViewModel.

どのように私はそれぞれのアイテムの名前プロパティを参照できますか?

+1

'ListBox'で' DisplayMemberPath = "Name" 'を試してください – Bijington

+0

私はやったけど、それは常にMainViewModelで見つけようとしています。 – user2837961

+0

'MyItemViewModel'クラスに' Name'プロパティがありますか? – Bijington

答えて

0

は変更してみてください:

<ListBox ItemsSource="{Binding MyList}" DisplayMemberPath="{Binding MyList.Name}"> 

へ:

<ListBox ItemsSource="{Binding MyList}" DisplayMemberPath="{Binding Name}"> 

または追加してみてください:

<Page.Resources> 
    <local:MyItemViewModel x:Key="DataContext" /> 
</Page.Resources> 

<ListBox DataContext="{StaticResource DataContext}" ItemsSource="{Binding MyList}" DisplayMemberPath="{Binding MyList.Name}"> </ListBox> 

通常私は、XAMLファイル自体にViewModelにを追加します。

<Page.Datacontext> 
    <local:MyItemViewModel/> 
</Page.Datacontext> 
+0

誰かが同じ提案でもっと速くなったようだ。 – Developer

+0

は新しいViewModelを作成しませんか? OPは既に存在するものを参照したいと思うようです。 – Default

+0

よろしいですか?それを捕まえていませんでした。 – Developer

2

は、おそらくBindingではありません。すなわち"Name"

<ListBox ItemsSource="{Binding MyList}" DisplayMemberPath="Name" /> 

、表示したいプロパティのプロパティ名に変更してくださいあなたは、インデントの挙動を得ることができますので、また、一般的な提案は、not to have public setters on listsです。代わりに、バッキングフィールドを使用してセッターを削除します。

関連する問題