My WPF applications have a demand, there is a list of ListBox, display a variety of fruits, assumptions are not repeated, in a TextBox text box input: bananas, click on the button bananas can be added to the list above, now need to set just added bananas in c # this item is selected,
Bananas, for example, just the actual may be any fruit that is not repeated,
Simple said is how to use the ListBox shows the value of the selected item?
CodePudding user response:
Query ItemContainerGenerator canCodePudding user response:
Thank you very much for reply, I also found this, but not so will write specific code, is to use ContainerFromItem (Object) method? Hope to be able to under the guidance of,CodePudding user response:
Which master can give some Suggestions and demonstration, hope to be able to smooth and stick to the pointCodePudding user response:
To the ListBox. ItemsSource and ListBox. SelectedValue are Binding, the AddCommand piece manually changing the SelectedValue Binding propertiesCodePudding user response:
The first method:1, Item (fruits) adding an IsSelected ItemModel attributes, and bind to ListBoxItem. An IsSelected
2, found in the data source to the selected fruit
3, find fruit. An IsSelected=true;
The second method:
1, binding the ListBox SelectedValue property
2, found in the data source to the selected fruit
3, will find fruit assigned to SelectedValue
CodePudding user response:
ListBoxItem lstitem=SessionListBox. ItemContainerGenerator. ContainerFromItem (SessionListBox. SelectedItem) as ListBoxItem;