2011-03-18 217 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