Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Getting an Item Template to raise selected event properly

Getting an Item Template to raise selected event properly

Scheduled Pinned Locked Moved WPF
wpfhelpquestionworkspace
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Michael Eber
    wrote on last edited by
    #1

    Here is my setup: I have a ListBox which contains an ItemTemplate. The ItemTemplate incorporates a checkbox with name/checked state bound to the ItemSource object. issue: when the checkbox is checked (or unchecked) the selected event is not picked up. Basically you have to click the checkbox text on the far right to actually select it. This can be a rather delecate issue as a user may think they are setting values for the item they checked but actually changing values for a previously selected item. Is there something in the xaml I can set or do I handle the checked event and set that object to selected item?

    M 1 Reply Last reply
    0
    • M Michael Eber

      Here is my setup: I have a ListBox which contains an ItemTemplate. The ItemTemplate incorporates a checkbox with name/checked state bound to the ItemSource object. issue: when the checkbox is checked (or unchecked) the selected event is not picked up. Basically you have to click the checkbox text on the far right to actually select it. This can be a rather delecate issue as a user may think they are setting values for the item they checked but actually changing values for a previously selected item. Is there something in the xaml I can set or do I handle the checked event and set that object to selected item?

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      If I'm reading correctly - the checkbox button is eating the mouse click events (like buttons do) so the listbox selection doesn't change, correct? If so, I would try adding Checked/Unchecked event handlers to the CheckBox. In the handlers, the sender parameter will be the CheckBox. The CheckBox's DataContext should be a reference to the selected item in the ListBox's ItemsSource. Set the listBox's SelectedItem property to the CheckBox's DataContext value. (Note I didn't test this :))

      private void CheckBox\_Checked(object sender, RoutedEventArgs e)
      {
          myListBox.SelectedItem = (sender as CheckBox).DataContext;
      }
      
      private void CheckBox\_Unchecked(object sender, RoutedEventArgs e)
      {
          myListBox.SelectedItem = (sender as CheckBox).DataContext;
      }
      

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      M 1 Reply Last reply
      0
      • M Mark Salsbery

        If I'm reading correctly - the checkbox button is eating the mouse click events (like buttons do) so the listbox selection doesn't change, correct? If so, I would try adding Checked/Unchecked event handlers to the CheckBox. In the handlers, the sender parameter will be the CheckBox. The CheckBox's DataContext should be a reference to the selected item in the ListBox's ItemsSource. Set the listBox's SelectedItem property to the CheckBox's DataContext value. (Note I didn't test this :))

        private void CheckBox\_Checked(object sender, RoutedEventArgs e)
        {
            myListBox.SelectedItem = (sender as CheckBox).DataContext;
        }
        
        private void CheckBox\_Unchecked(object sender, RoutedEventArgs e)
        {
            myListBox.SelectedItem = (sender as CheckBox).DataContext;
        }
        

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        M Offline
        M Offline
        Michael Eber
        wrote on last edited by
        #3

        Mark, Yup, you understood correctly, the checkbox is gobbling up the event and the listbox does not select the item. I pretty much thought I'd have to do what you suggested, setting listbox.SelectedItem to the corresponding checkbox. Thanks Mark, Michael

        M 1 Reply Last reply
        0
        • M Michael Eber

          Mark, Yup, you understood correctly, the checkbox is gobbling up the event and the listbox does not select the item. I pretty much thought I'd have to do what you suggested, setting listbox.SelectedItem to the corresponding checkbox. Thanks Mark, Michael

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          I added a code sample to my post....does it work?

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          M 1 Reply Last reply
          0
          • M Mark Salsbery

            I added a code sample to my post....does it work?

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            M Offline
            M Offline
            Michael Eber
            wrote on last edited by
            #5

            yuppers, that worked. :cool:

            M 1 Reply Last reply
            0
            • M Michael Eber

              yuppers, that worked. :cool:

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              Good to know, thanks!

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups