Setting a textbox based on listbox selection.
-
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:
-
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:
-
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
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.
-
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.
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 theSelectionChanged
-event of the ListBox. The type of the SelectedItem depends on the givenItems
(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> asItemsSource
, you get a string as SelectedItem. As mentioned by Abhinav S you might want to consider using theIsSynchronizedWithCurrentItem
property. Cheers Jürgen