Home > other >  How WPF ListBox display value according to the Settings selected item?
How WPF ListBox display value according to the Settings selected item?

Time:10-23

The small white have a question to ask:
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 can

CodePudding 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 point

CodePudding user response:

To the ListBox. ItemsSource and ListBox. SelectedValue are Binding, the AddCommand piece manually changing the SelectedValue Binding properties

CodePudding 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;
  • Related