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();
難道我甚至在正確的軌道?任何指導將不勝感激。
現在,如果只有堆棧溢出的每個答案都做得很好。謝謝!我得說我正在努力與WPF(即使它的意思是更簡單)PS:我已經注意到,大多數試圖做類似的事情的代碼使用INotifyPropertyChanged。你能解釋爲什麼這個代碼需要嗎? – 2011-03-18 03:16:17