none
listbox selected item as data source for new window RRS feed

  • Question

  • 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) 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



    • Edited by ajk825 Tuesday, December 18, 2012 5:03 AM
    Sunday, December 16, 2012 2:34 PM

All replies

  • Hi Adam,

    Welcome to the MSDN forum.

    I am not very familiar with WPF, so I am trying to involve a senior expert in your thread. Please wait for the response. Sorry for any inconvenience.

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 18, 2012 4:41 AM
  • Hi Adam,

    The SelectedItem is a single item, not a collection, so you probably just need to either call ProjectsData.Add(selitem) instead, or maybe add the selitem to a new empty collection and assign that to ProjectsData, if ProjectsData is not previously initialized.  That should take care of the error then I expect.

    Jon Burchel

    • Proposed as answer by Jon Burchel Friday, December 21, 2012 7:30 PM
    Friday, December 21, 2012 7:30 PM
  • Hi Jon,

       Thanks for the reply. I'm trying this now:

     Private Sub btnSelect_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnSelect.Click
            Dim mychoice As New MyChoice
            Dim selitem As Object
            selitem = MyLists.SelectedItem
            mychoice.ProjectsData.Add(selitem)
            mychoice.Show()
            Me.Close()
        End Sub

    unfortunately i'm getting this error:

    System.NullReferenceException was unhandled
      Message=Object reference not set to an instance of an object.
      Source=Tests
      StackTrace:
           at Tests.SearchText.btnSelect_Click(Object sender, RoutedEventArgs e) in C:\Users\ours\Desktop\Test\Tests\Tests\SearchText.xaml.vb:line 73
           at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
           at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
           at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
           at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
           at System.Windows.Controls.Primitives.ButtonBase.OnClick()
           at System.Windows.Controls.Button.OnClick()
           at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
           at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
           at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
           at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
           at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
           at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
           at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
           at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
           at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
           at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
           at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
           at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
           at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
           at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
           at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
           at System.Windows.Input.InputManager.ProcessStagingArea()
           at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
           at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
           at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
           at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
           at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
           at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
           at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
           at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
           at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
           at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
           at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
           at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
           at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
           at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
           at System.Windows.Application.RunDispatcher(Object ignore)
           at System.Windows.Application.RunInternal(Window window)
           at System.Windows.Application.Run(Window window)
           at System.Windows.Application.Run()
           at Tests.Application.Main() in C:\Users\ours\Desktop\Test\Tests\Tests\obj\x86\Debug\Application.g.vb:line 64
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException:
     

    any suggestions?


    Adam

    Friday, December 21, 2012 7:43 PM
  • Hi Ajk825,

    Did you try this:

    MyChoice mychoice = new MyChoice { ProjectsData = new List<Object>() };

    mychoice.ProjectsData.Add(selitem)

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 10, 2013 8:51 AM