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. Setting a textbox based on listbox selection.

Setting a textbox based on listbox selection.

Scheduled Pinned Locked Moved WPF
csharpwpfquestion
4 Posts 3 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.
  • J Offline
    J Offline
    jhoga
    wrote on last edited by
    #1

    This should be easy but I can't get to work. I just want to set the value of a textbox when the select value in a list box changes. Here's how it works in a combobox. Category.Text = ComboBox1.Text How does it work in a list box in vb.2010 (WPF)? :confused:

    A 1 Reply Last reply
    0
    • J jhoga

      This should be easy but I can't get to work. I just want to set the value of a textbox when the select value in a list box changes. Here's how it works in a combobox. Category.Text = ComboBox1.Text How does it work in a list box in vb.2010 (WPF)? :confused:

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      Have a look here. The equivalent of Category.Text = ComboBox1.Text is a little different in WPF. You need to link the CurrentItem of a listbox to your textbox. The link above might give you a better idea.

      My signature "sucks" today

      J 1 Reply Last reply
      0
      • A Abhinav S

        Have a look here. The equivalent of Category.Text = ComboBox1.Text is a little different in WPF. You need to link the CurrentItem of a listbox to your textbox. The link above might give you a better idea.

        My signature "sucks" today

        J Offline
        J Offline
        jhoga
        wrote on last edited by
        #3

        Thats good info, but I really do not want to databind that textbox. I'm binding back to SQl already I just want to give the user an easy way to populate/change the field value. The combo works, but a list box looks much better. I'm making it visilble from a click event. Thanhks for the info.

        J 1 Reply Last reply
        0
        • J jhoga

          Thats good info, but I really do not want to databind that textbox. I'm binding back to SQl already I just want to give the user an easy way to populate/change the field value. The combo works, but a list box looks much better. I'm making it visilble from a click event. Thanhks for the info.

          J Offline
          J Offline
          Jurgen Rohr
          wrote on last edited by
          #4

          Hi, you must convert the SelectedItem of the ListBox into the string you need. You can do this by binding it to another property (of your viewmodel) and in the new property's setter, you can transfer the converted string-value to your TextBox.Text-property. Alternatively you must code the SelectionChanged-event of the ListBox. The type of the SelectedItem depends on the given Items (sounds a bit stupid, I know). If you fill the ListBox at designtime, you will probably have something like

          <ListBox ... >
          <ListBox.Items>
          <ListBoxItem Content="one" />
          <ListBoxItem Content="two" />
          <ListBoxItem Content="three" />
          </ListBox.Items>
          </ListBox>

          In this case, the SelectedItem is of type ListBoxItem. If you e. g. provide a List<string> as ItemsSource, you get a string as SelectedItem. As mentioned by Abhinav S you might want to consider using the IsSynchronizedWithCurrentItem property. Cheers Jürgen

          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