Skip to content

Commit d3d8d8e

Browse files
985360: Updated ReadMe file of this repository
1 parent 32fb147 commit d3d8d8e

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,33 @@
11
# How-to-bind-SelectedItem-property-in-Winforms-SfComboBox
2-
This session explains how to  bind SelectedItem property in Winforms SfComboBox
2+
This example demonstrates how to **bind the SelectedItem property** of the **Syncfusion WinForms SfComboBox** control to another property in your application. Binding the SelectedItem property is useful when you want to synchronize the selected value in the combo box with a property in your data model or view model. This ensures that any changes in the UI are reflected in the underlying data and vice versa.
3+
4+
## Why Use Data Binding?
5+
Data binding simplifies the process of keeping the UI and data model in sync. When you bind the SelectedItem property:
6+
- The selected item in the combo box updates automatically when the bound property changes.
7+
- Any user selection in the combo box updates the bound property without additional code.
8+
9+
## Implementation Steps
10+
1. **Prepare the ViewModel**: Create a ViewModel class with a collection of items and a property to hold the selected item.
11+
2. **Configure SfComboBox**:
12+
- Set the `DataSource` to the collection of items.
13+
- Define `DisplayMember` and `ValueMember` for proper display and value mapping.
14+
3. **Bind SelectedItem**:
15+
- Use the `DataBindings.Add` method to bind the SelectedItem property of SfComboBox to the ViewModel property.
16+
17+
## Example Code
18+
```csharp
19+
public Form1()
20+
{
21+
InitializeComponent();
22+
ViewModel viewModel = new ViewModel();
23+
sfComboBox1.DataSource = viewModel.Items;
24+
sfComboBox1.DisplayMember = "LongName";
25+
sfComboBox1.ValueMember = "ShortName";
26+
sfComboBox1.DataBindings.Add(new Binding("SelectedItem", viewModel, "Selected", true, DataSourceUpdateMode.OnPropertyChanged));
27+
}
28+
```
29+
30+
This code binds the `SelectedItem` property of `sfComboBox1` to the `Selected` property in the ViewModel. The binding uses `DataSourceUpdateMode.OnPropertyChanged` to ensure updates occur immediately when the property changes.
31+
32+
For more details, refer to the official Syncfusion Knowledge Base article:
33+
🔗 [How to Bind SelectedItem Property in SfComboBox](https://www.syncfusion.com/kb/11866/how-to-bind-selecteditem-property-of-sfcombobox-to-another-property)

0 commit comments

Comments
 (0)