2011-03-18 214 views
0

我正在嘗試在WPF表單中編寫我的第一個數據綁定實現。將參數傳遞給CollectionViews

我已經到了可以成功檢索數據以填充所需條目的組合框的位置,但是我堅持要如何在給定組合框的當前選擇的情況下填充相關ListView。

爲了更好地解釋我的問題,這是支持我的應用程序的當前視圖。

class SchoolBookViewModel 
{ 
    public SchoolBookViewModel() { } 

    //Problem is here... How do I pass in the SelectedSchoolClassID? 
    public CollectionView ClassBookListEntries(Guid SelectedSchoolClassID) 
    { 
     return new CollectionView(SchoolQuoteList.GetSchoolClassQuoteListBySchoolClassID(SelectedSchoolClassID, 2011, "MyConnectionString")); 
    } 

    public CollectionView SchoolEntries 
    { 
     get 
     { 
      return new CollectionView(SchoolList.GetSchoolList("MyConnectionString")); 
     } 
    } 

} 

這是XAML

<StackPanel Height="449" HorizontalAlignment="Left" Margin="12,12,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="650" DataContext="{Binding}"> 
     <Label Content="School:" Height="28" Name="lblSchoolName" Width="651" /> 
     <ComboBox Height="23" Name="cmbSchoolNames" Width="644" DisplayMemberPath="SchoolName" ItemsSource="{Binding Path=SchoolEntries}" SelectedValuePath="SelectedSchoolClassID" SelectionChanged="cmbSchoolNames_SelectionChanged" /> 
     <Label Content="Class booklist:" Height="29" Name="label1" Width="651" /> 
     <ListView Height="163" Name="lblClassBookList" Width="645" ItemsSource="{Binding Path=ClassBookListEntries}" DisplayMemberPath="QuoteReference" /> 
</StackPanel> 

而在Window_Loaded方法我稱之爲

stackPanel1.DataContext = new Views.SchoolBookViewModel(); 

難道我甚至在正確的軌道?任何指導將不勝感激。

回答

1

要將ComboBox的選擇返回到ViewModel,您需要一個屬性來綁定到其選擇屬性之一。如果你沒有對它們做任何事情,你也可以擺脫顯式的CollectionViews。通過直接綁定到集合,ICollectionView實例將自動爲您創建和管理。嘗試構建你的虛擬機是這樣的:

class SchoolBookViewModel : INotifyPropertyChanged 
{ 
    private SchoolList _selectedSchoolClass; 
    public SchoolList SelectedSchoolClass 
    { 
     get { return _selectedSchoolClass; } 
     set 
     { 
      _selectedSchoolClass = value; 
      ClassBookListEntries = SchoolQuoteList.GetSchoolClassQuoteListBySchoolClassID(_selectedSchoolClass.Id, 2011, "MyConnectionString"); 
      NotifyPropertyChanged("SelectedSchoolClass"); 
     } 
    } 

    private SchoolQuoteList _classBookListEntries; 
    public SchoolQuoteList ClassBookListEntries 
    { 
     get { return _classBookListEntries; } 
     set 
     { 
      _classBookListEntries = value; 
      NotifyPropertyChanged("ClassBookListEntries"); 
     } 
    } 

    private SchoolList _schoolEntries; 
    public SchoolList SchoolEntries 
    { 
     get 
     { 
      if (_schoolEntries == null) 
       _schoolEntries = SchoolList.GetSchoolList("MyConnectionString"); 
      return _schoolEntries; 
     } 
    } 
    ... 
} 

一般來說,最好不要設置明確的寬度和高度值,而是讓佈局系統大小元素你。您還可以擺脫DataContext =「{Binding}」 - 這是多餘的,因爲DataContext是繼承的,{Binding}意味着DataContext本身的值。這裏是XAML清理並綁定到上面的新屬性:

<StackPanel HorizontalAlignment="Left" Margin="12,12,0,0" x:Name="stackPanel1" VerticalAlignment="Top"> 
    <Label Content="School:" x:Name="lblSchoolName" /> 
    <ComboBox x:Name="cmbSchoolNames" DisplayMemberPath="SchoolName" ItemsSource="{Binding Path=SchoolEntries}" 
       SelectedItem="{Binding SelectedSchoolClass}" /> 
    <Label Content="Class booklist:" x:Name="label1" /> 
    <ListView x:Name="lblClassBookList" ItemsSource="{Binding Path=ClassBookListEntries}" DisplayMemberPath="QuoteReference" /> 
</StackPanel> 
+0

現在,如果只有堆棧溢出的每個答案都做得很好。謝謝!我得說我正在努力與WPF(即使它的意思是更簡單)PS:我已經注意到,大多數試圖做類似的事情的代碼使用INotifyPropertyChanged。你能解釋爲什麼這個代碼需要嗎? – 2011-03-18 03:16:17