将参数传递给CollectionViews

时间:2011-03-18 01:45:32

标签: c# wpf model-view-controller design-patterns binding

我试图用WPF格式编写我的第一个数据绑定实现。

我已经到了能够成功检索数据以使用所需条目填充我的组合框的那一点,但我仍然坚持如何在给出当前选择的Combobox的情况下填充依赖的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 个答案:

答案 0 :(得分:1)

要将ComboBox的选择重新放回ViewModel,您需要一个属性来绑定到其选择属性之一。如果你没有对它们做任何事情,你也可以摆脱显式的CollectionViews。通过直接绑定到集合,将自动为您创建和管理ICollectionView实例。尝试像这样构建您的VM:

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;
        }
    }
    ...
}

一般来说,最好不要设置显式的Width和Height值,而是让布局系统大小元素。你也可以摆脱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>