Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Sep 2009
    Messaggi
    59

    [C# WPF] Children of children

    Verificatosi un evento devo identificare quale checkbox è stata selezionata. Queste checkbox sono interne a dei Canvas che a loro volta sono inserite nella Grid principale. Ho quindi scritto questo doppio ciclo:

    codice:
    foreach (Canvas canvas in Grid.Children) { 
      foreach (CheckBox chechbox in canvas.Children) { 
          if (chechbox.IsChecked == true) { 
            checkbox_selected = chechbox.Name; 
    } } }
    ma mi viene restituito questo errore:

    codice:
    System.InvalidCastException non è stata gestita
     Message=Unable to cast object of type 'System.Windows.Controls.Button' to type 'System.Windows.Controls.Canvas'.
     Source=Elettroporazione
     StackTrace:
        at Elettroporazione.Page1.buttonSave_Click(Object sender, RoutedEventArgs e) in C:\Users\Notebook\Documents\Lavoro FBK\2011\Elettroporazione\Elettroporazione\Page1.xaml.cs:line 70
        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.Threading.Dispatcher.Run()
        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 Elettroporazione.App.Main() in C:\Users\Notebook\Documents\Lavoro FBK\2011\Elettroporazione\Elettroporazione\obj\x86\Debug\App.g.cs:line 0
        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:
    riferito al primo foreach, cosa sbaglio in questo? Grazie!

  2. #2
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,296

    Moderazione

    I linguaggi Visual Basic 6, Visual Basic .NET, C# e altri linguaggi basati sul .NET Framework sono trattati nell'area dedicata.

    Sposto la discussione.
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

  3. #3
    Utente di HTML.it L'avatar di ShaleN
    Registrato dal
    Aug 2010
    Messaggi
    517
    Con questo codice, se in Grid.Children trova un oggetto diverso da un Canvas tenta comunque di convertirlo a Canvas, causando quell'eccezione. Prova con questo codice:

    codice:
    foreach (Object canvas in Grid.Children) { 
      if (TypeOf(canvas) Is Canvas)
        foreach (Object chechbox in (Canvas)canvas.Children) { 
            if (TypeOf(chechbox) Is Chechbox && (Chechbox)chechbox.IsChecked == true) { 
                checkbox_selected = (Chechbox)chechbox.Name; 
            }
         } // foreach (Object chechbox in (Canvas)canvas.Children)
       } // if (TypeOf(canvas) Is Canvas)
     } // foreach (Object canvas in Grid.Children)
    Le vie del Signore sono infinite. È la segnaletica che lascia a desiderare.
    La luce viaggia più veloce del suono. Per questo alcune persone sembrano brillanti finchè non parlano.
    Occhio per occhio uguale... occhio al quadrato

  4. #4

    WPF ciclo foreach in controlli form

    Buongiorno a tutti,
    scrivo in questa discussione perché mi sembra la più attinente al mio problema.
    io volevo ciclare tutti i controlli nella mia applicazione WPF e nel codice (C#) ho scritto come si fa di solito:

    foreach (Control Ctrl in Prima_Registrazione )
    {
    //Codice da completare
    }

    ora mi da come errore sottolineando in rosso "Prima_Registrazione" il seguente errore:
    Prima_Registrazione è un tipo; che non è costrutto valido nel contesto specificato.

    premetto che questo tipo di ciclo dei controlli nella form l'ho fatto più volte ed ha sempre funzionato, questa è la prima volta che mi capita.

    Spero riusciate ad aiutarmi

    grazie

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.