WPF Combo Box gets the selected tag

Source: Internet
Author: User

string str1 = ((ComboBoxItem)this. cboboxrate1553b.items[. Cboboxrate1553b.selectedindex]). Tag.tostring (); string str2 = ( this as ComboBoxItem). Tag.tostring ();

I do not know why I can only convert to var or string type, and then to other types.

I have a combo box like this

    <ComboBox Name="myMenu">        <ComboBoxItem Content="Question 1" Tag="1"  />        <ComboBoxItem Content="Question 2" Tag="2"  />        <ComboBoxItem Content="Question 3" Tag="3"  />        <ComboBoxItem Content="Question 4" Tag="4"  />    </ComboBox>

How can I programmatically set the selected index by Tag Value? e.g. ' Mymenu.selectedtag = 3 ' and Question 3 would be the selected item?

I want something easier than my current solution really ...

      int tagToSelect = 3;      foreach (ComboBoxItem item in myMenu.Items)      {          if(item.Tag.Equals(tagToSelect)          {               myMenu.SelectedItem = item;          }      }

WPF Combo Box gets the selected tag

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.