2017-01-12 1 views
-1

私は、N層wpf mvvmプロジェクトを作成しています。私はリストボックスに表示されるデータベースからアイテムのリストを取得したい。リストボックスをViewModel(VM)のプロパティにバインドしたいと思います。問題は、バインディングが私のために働いていない、リストボックスは常に空です。 "return tags"にブレークポイントを設定すると、フォームが表示される前に完全に読み込まれます。WPFリストボックスバインディングの問題

MainWindow()コンストラクタのDataContext = App.ViewModel;

私のVMでは 私のXAMLで

<ListBox ItemsSource="{Binding Tags, Mode=OneWay}" Height="161" HorizontalAlignment="Left" Margin="236,6,0,0" Name="lstTags" VerticalAlignment="Top" Width="130" > 
      <ListBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Vertical"> 
          <TextBlock Text="{Binding Tags.Name}" /> 
          <TextBlock Text="{Binding Tags.Description}" /> 
          <!--<CheckBox IsChecked="{Binding Deleted, Mode=TwoWay}"/>--> 
         </StackPanel> 
        </DataTemplate> 
      </ListBox.ItemTemplate> 
    </ListBox> 

:私はそれがフル装備である「タグを返す」にブレークポイントを置きます。

private TagCol _tags; 
public TagCol Tags 
{ 
    get { 
     TagColData tcd = new TagColData(); 
     _tags = tcd.LoadAll(); 

     //NotifyPropertyChanged("Tags"); 

     return _tags; 
    } 
    set { 
     _tags = value; 
     NotifyPropertyChanged("Tags"); 
    } 
} 

TagCol:

パブリッククラスTagCol {プライベートのObservableCollection _tagCol =新規のObservableCollection()。

/// <summary>Collection (list) of Tag objects</summary> 
public ObservableCollection<Tag> Collection { 
    get { return _tagCol; } 
    set 
    { 
     _tagCol = value; 
    } 
} 

public TagCol() 
{ 

} 

}

+0

MainWindow(またはListBoxの親コントロール)のDataContextをビューモデルクラスのインスタンスに設定しましたか? – Clemens

+0

'TagCol 'の定義を含めてください。 –

+0

実行時にバインドとコントロールを調べるには、Snoopを使用してください。これを行うには、TagColがIEnumerableを実装する必要があります。 – Will

答えて

0

みんなそれはCollection.Nameが動作しないテキストボックス用のサブプロパティで、Tags.Collectionなお に設定するために必要な、結合に当然でした。

さて、数日の余裕がありました。 :)

 <ListBox ItemsSource="{Binding Tags.Collection}" Height="161" HorizontalAlignment="Left" Margin="236,6,0,0" Name="lstTags" VerticalAlignment="Top" Width="130" > 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Vertical"> 
         <TextBlock Text="{Binding Path=Name}" /> 
         <TextBlock Text="{Binding Path=Description}" /> 
         <CheckBox IsChecked="{Binding Deleted, Mode=TwoWay}"/> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox>