Articles in this section
Category / Section

How to bind SelectedItem property of SfComboBox to another property ?

1 min read

You can bind the SelectedItem property of SfComboBox control by using the DataBindings.Add() method. The below code shows how to bind the SelectedItem with the Selected property in ViewModel.cs.

State.cs:

public class State
{
      private string shortName;
      private string longName;
 
      public State(string LongName, string ShortName)
      {
          this.longName = LongName;
          this.shortName = ShortName;
      }
 
      public string ShortName
      {
          get { return shortName; }
      }
 
      public string LongName
      {
          get { return longName; }
      }
}

 

ViewModel.cs:

public class ViewModel
{
     private List<State> items;
 
     public List<State> Items
     {
           get { return items; }
           set { items = value; }
     }
 
     private object selected;
 
     public object Selected
     {
           get { return selected; }
           set { selected = value; }
      }
 
     public ViewModel()
     {
           Items = new List<State>();
           Items.Add(new State("Alaska", "AK"));
           Items.Add(new State("Arizona", "AZ"));
           Items.Add(new State("Colorado", "CO"));
           Items.Add(new State("Ontario", "ON"));
           Items.Add(new State("BritishColumbia", "BC"));
 
            Selected = Items[3];
     }
}

 

 

Form1.cs:

public partial class Form1 : Form
{
      public Form1()
      {
            InitializeComponent();
 
            ViewModel viewModel = new ViewModel();
 
            sfComboBox1.DataSource = viewModel.Items;
            sfComboBox1.DisplayMember = "LongName";
            sfComboBox1.ValueMember = "ShortName";
 
            sfComboBox1.DataBindings.Add(new Binding("SelectedItem", viewModel,"Selected",true, DataSourceUpdateMode.OnPropertyChanged));
 
      }
}

SfComboBox SelectedItem with DataBinding

 

View sample in GitHub

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied