列表框选中的项目作为新窗口的数据源 [英] listbox selected item as data source for new window

查看:63
本文介绍了列表框选中的项目作为新窗口的数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的是一个使用Entity Framework 4.0的WPF应用程序。我有一个Entity DataModel(CraftingProjectsEntities)绑定到一个Observable Collection(ProjectsCollection)和一些CollectionViewSources。在我的表单上,我有两个文本框i
用于过滤我的数据,然后根据我过滤它的内容将其显示到列表框中。我需要做的是让用户从列表框中选择一个项目并点击一个按钮("选择")并根据我在列表框中单击的项目打开一个新表单,
传递新表单来自所选项目文本块的数据。我该怎么做?我没有使用MVVM,如果这有帮助......

I have a WPF application I am working on that uses the Entity Framework 4.0. I have an Entity DataModel (CraftingProjectsEntities) bound to an Observable Collection (ProjectsCollection) and a few CollectionViewSources. On my form i have two text boxes i use to filter my data and then display it into a listbox based on what I have filtered it by. What I need to do is to have the user select an item from the listbox and hit a button ("Select") and open a new form based on the item I clicked on the listbox, passing the new form the data from the selected item's textblock. how do i do that? I am not using MVVM if this helps...

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)处理btnSelect.Click
Dim mychoice As新的MyChoice
Dim selitem = Me.MyLists.SelectedItem 这是我收到错误的地方:System.InvalidCastException未处理
  Message =无法将'Tests.Project'类型的对象强制转换为'Tests.ProjectsCollection'。
mychoice.ProjectsData =(selitem)
mychoice.Show()
Me .Close()
End Sub
End Class

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 This is where i get an error: System.InvalidCastException was unhandled
  Message=Unable to cast object of type 'Tests.Project' to type 'Tests.ProjectsCollection'. mychoice.ProjectsData = (selitem) mychoice.Show() Me.Close() End Sub End Class


关于如何做到这一点的任何想法或我在哪里找到一个显示正确方法的例子这样做将非常感激。谢谢。


Any thoughts on how I could do this or where I could find an example showing the proper way of doing this would be extremely appreciated. Thanks.

Adam

推荐答案

嗨Adam,

欢迎来到MSDN论坛。

我对WPF不是很熟悉,所以我想让你的线程中有一位资深专家。请等待回复。很抱歉给您带来不便。

有一个美好的一天。


这篇关于列表框选中的项目作为新窗口的数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆