列表框选中的项目作为新窗口的数据源

时间:2012-12-16 01:45:24

标签: wpf entity-framework-4

我正在使用的WPF应用程序使用Entity Framework 4.0一个Observable Collection和一些CollectionViewSources。在我的表单上,我有两个文本框,我用来过滤我的数据,然后根据我过滤它的内容将其显示到列表框中。我需要做的是让用户从列表框中选择一个项目并点击一个按钮(“选择”),并在一个全新的窗口中显示具有适当集合的信息。     到目前为止,我有:

Imports System.Data.Objects

Public Class SearchText

Private db As New CraftingProjectsEntities
Private ProjectsData As ProjectsCollection

Private ProjectViewSource As CollectionViewSource
Private MaterialsViewSource As CollectionViewSource
Private ColoringsViewSource As CollectionViewSource
Private DirectionsViewSource As CollectionViewSource
Private SpecialsViewSource As CollectionViewSource

Private WithEvents ProjectView As ListCollectionView
Private MaterialsView As BindingListCollectionView
Private ColoringsView As BindingListCollectionView
Private SpecialsView As BindingListCollectionView
Private DirectionsView As BindingListCollectionView

Private Sub SearchText_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
    Dim query = From r In db.Projects
                                  Where r.Materials.Count > 1
                             Select r

     Me.ProjectViewSource = CType(Me.FindResource("ProjectsViewSource"), CollectionViewSource)
    Me.DirectionsViewSource = CType(Me.FindResource("DirectionsViewSource"), CollectionViewSource)
    Me.ColoringsViewSource = CType(Me.FindResource("ColoringsViewSource"), CollectionViewSource)
    Me.SpecialsViewSource = CType(Me.FindResource("SpecialsViewSource"), CollectionViewSource)
    Me.MaterialsViewSource = CType(Me.FindResource("MaterialsViewSource"), CollectionViewSource)
    Me.ProjectViewSource.Source = Me.ProjectsData

    Me.ProjectView = CType(Me.ProjectViewSource.View, ListCollectionView)
    Me.DirectionsView = CType(Me.DirectionsViewSource.View, BindingListCollectionView)
    Me.ColoringsView = CType(Me.ColoringsViewSource.View, BindingListCollectionView)
    Me.MaterialsView = CType(Me.MaterialsViewSource.View, BindingListCollectionView)
    Me.SpecialsView = CType(Me.SpecialsViewSource.View, BindingListCollectionView)


    MyLists.ItemsSource = ProjectsData

End Sub 

Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click

    Dim query2 As ObjectQuery(Of Project) = Nothing
    Dim query = From p In db.Projects
                Select p

    If TextBox2.Text.Length > 1 And TextBox1.Text.Length > 1 Then

        query2 = query.Where(Function(p) p.Materials.Any(Function(m) m.Material1.ToLower().Contains(Me.TextBox1.Text)) And
                 p.Materials.Any(Function(m) m.Material1.ToLower().Contains(Me.TextBox2.Text)))

    ElseIf TextBox2.Text.Length < 1 And TextBox1.Text.Length > 1 Then

        query2 = query.Where(Function(p) p.Materials.Any(Function(m) m.Material1.ToLower().Contains(Me.TextBox1.Text)))

    Else
        MessageBox.Show("What are you looking for?")
        Return
    End If

    Me.ProjectsData = New ProjectsCollection(query2.ToList(), db)
    MyLists.ItemsSource = ProjectsData

  End Sub
                           **Works great through here... Below is where i get lost trying to get the selected item to the new window**
  Private Sub btnSelect_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnSelect.Click
    Dim mychoice As New MyChoice
    Dim selitem = Me.MyLists.SelectedItem
    mychoice.ProjectsData = (selitem)
    mychoice.Show()
    Me.Close()
End Sub
End Class

关于我如何做到这一点的任何想法,或者我可以找到一个显示正确方法的例子,我将非常感激。谢谢。 亚当 抱歉....如果有帮助,不要使用MVVM。

1 个答案:

答案 0 :(得分:1)

使模型Project成为MyChoice窗口类的参数。根据窗口是否需要,将Project设置为MyChoice构造函数的必需参数或可选参数。将其设置为新窗口的布局根的数据上下文,将其设置为Window类本身的属性,或将其放在View Model中;以适当者为准。