将参数传递给 CollectionViews

发布于 2024-10-23 20:14:33 字数 1645 浏览 1 评论 0原文

我正在尝试以 WPF 形式编写数据绑定的第一个实现。

我已经到了可以成功检索数据以使用所需条目填充组合框的地步,但我仍坚持如何在给定组合框当前选择的情况下填充依赖的 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(); 

我是否走在正确的轨道上?任何指导将不胜感激。

I am attempting to write my first implementation of databinding in a WPF form.

I have got to the point where I can successfully retrieve data to populate my combobox with the required entries but am stuck on how I would go about populating a dependent ListView given the current selection of the Combobox.

To explain my problem better this is the current view backing my application.

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"));
        }
    }

}

And this is the 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>

And in the Window_Loaded method I call

stackPanel1.DataContext = new Views.SchoolBookViewModel(); 

Am I even on the right track? Any guidance would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一影成城 2024-10-30 20:14:33

要将 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>

To get the ComboBox's selection back into the ViewModel you need a property to bind to one of its selection properties. You can also get rid of the explicit CollectionViews if you're not doing anything with them. By just binding to the collections directly, ICollectionView instances will be created and managed for you automatically. Try structuring your VM like this:

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

In general it's better to not set explicit Width and Height values and instead let the layout system size elements for you. You can also get rid of the DataContext="{Binding}" - this is redundant as DataContext is inherited and {Binding} means the value of the DataContext itself. Here's the XAML cleaned up and bound to the new properties from above:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文