17478

Set combobox Item in code Behind UWP

Question:

I have seen how to select the item from the index by code behind, but how can i select it from code behind knowing the string of the item?

combobox code xaml:

<ComboBox x:Name="ComboBoxOne" VerticalAlignment="Center" HorizontalAlignment="Center" Height="40" Width="200"> <ComboBoxItem Content="blue"/> <ComboBoxItem Content="red"/> <ComboBoxItem Content="green"/> </ComboBox>

combobox code behind:

ComboBoxOne.SelectedIndex = 1;

But how to select the item knowing for example green? Is possible?

I tried with ComboBoxOne.PlaceholderText

ComboBoxOne.PlaceholderText="green"

But then I can not use the selecteditem.

Thanks in advance!

Answer1:

First you need to get the Items of the ComboBox as a List to find the Index of the item that you want to select by string. Since this will be a List<String> you can do something like below.

List<String> lstItems = ComboBoxOne.Items .Cast<ComboBoxItem>() .Select(item => item.Content.ToString()) .ToList();

and then you can get the index using Linq and assign it to Selected Index. Like below.

ComboBoxOne.SelectedIndex = lstItems.FindIndex(a => a.Equals("green"));

Good Luck.

Recommend

  • sas drop value through the whole data
  • Jquery assign value to JSP variable
  • How can this be done with (N)Hibernate?
  • Melt the data frame with 4 columns to three columns
  • A ListBoxItem that fills its parent
  • Flyout changes Page's theme
  • UWP XAML ImageBrush.imageSource from networkshare
  • Bind Xaml bitmap image
  • Binding Selected RowCount to TextBlock not Firing OnPropertyChanged after DataGrid Scroll
  • Get attributes of existing SVG elements and bind as data with d3.js
  • Setting color in a row of a Jtable
  • Stretch a span across a td
  • How can a flex item keep the same dimensions when it is forced to a new row?
  • UIButton Borders Function Only Gives Back White Borders
  • Set step size polar plot matplotlib python
  • Caliburn Micro, How to use ContentControl (or display 'sub' ViewModel) using ViewModel Fir
  • Responsive left sidebar open close
  • Plotting densities in R
  • blade.php method outputting it's result to the form
  • Update Google Maps traffic layer without page reloading
  • D3 get axis values on zoom event
  • Installing iPhone App to iPhone
  • How to modify the colors in the legend of a plot using a fill gradient?
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • OpenGL 3.3 on Mac OSX El Capitan with LWJGL
  • Highlight and Bold text in JTextPane
  • Xamarin Forms - UWP Fonts
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • How to set/get protobuf's extension field in Go?
  • How to show dropdown in excel using jrxml (jasper api)?
  • jquery mobile loadPage not working
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • JTable with a ScrollPane misbehaving
  • Android Google Maps API OnLocationChanged only called once
  • python draw pie shapes with colour filled
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?