0

我有一个我正在开发的 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。

4

1 回答 1

1

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

于 2012-12-16T01:51:20.910 回答