Skip to content

Commit 4992d1d

Browse files
authored
Merge pull request #2 from SyncfusionExamples/WF-64663-SfComboBox-DropDownItems
WF-64663-Changed ReadMe File.
2 parents c84b219 + a058fa4 commit 4992d1d

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

README.md

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,75 @@
1-
# How-to-add-separator-line-in-WinForms-SfComboBox-dropdown-items-
2-
This session describes how to add separator line in WinForms SfComboBox dropdown iems.
1+
# How to Add Separator Line in WinForms SfComboBox Dropdown Items
2+
## Overview
3+
This example demonstrates how to add a separator line to the dropdown items in a WinForms SfComboBox using Syncfusion controls. This customization is useful for visually grouping items or improving readability in complex dropdown lists.
4+
5+
## Implementation Details
6+
The requirement can be achieved by handling:
7+
- SfComboBox.DropDownListView.DrawItem event (to draw custom UI elements)
8+
- MouseMove event (to highlight hovered items)
9+
10+
### Key Steps:
11+
- Use reflection to set CheckBoxStyle dynamically.
12+
- Draw the checkbox, item text, and separator line manually.
13+
- Handle hover and selection states for better UX.
14+
15+
``` C#
16+
private void DropDownListView_DrawItem(object sender, Syncfusion.WinForms.ListView.Events.DrawItemEventArgs e)
17+
{
18+
bool isEnableDraw = (sfComboBox1.ComboBoxMode == ComboBoxMode.MultiSelection &&
19+
sfComboBox1.AllowSelectAll &&
20+
sfComboBox1.DropDownListView.ShowCheckBoxes &&
21+
e.ItemIndex == 0)
22+
? false
23+
: (e.ItemData as Student).IsDraWSeparator;
24+
25+
if (isEnableDraw)
26+
{
27+
e.Handled = true;
28+
var listView = sender as SfListView;
29+
30+
// Access internal layout and checkbox style using reflection
31+
var linearLayout = listView.GetType().GetProperty("LinearLayout", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(listView);
32+
var items = linearLayout.GetType().GetProperty("Items").GetValue(linearLayout) as List<ListViewItemInfo>;
33+
var checkBoxStyle = items[e.ItemIndex].GetType().GetProperty("CheckBoxStyle", BindingFlags.NonPublic | BindingFlags.Instance);
34+
checkBoxStyle.SetValue(items[e.ItemIndex], listView.Style.CheckBoxStyle);
35+
36+
// Highlight hover and selected items
37+
if (e.ItemIndex == mouseHoverItemIndex)
38+
e.Graphics.FillRectangle(new SolidBrush(listView.Style.SelectionStyle.HoverBackColor), e.Bounds);
39+
if (sfComboBox1.SelectedIndex == e.ItemIndex)
40+
e.Graphics.FillRectangle(new SolidBrush(listView.Style.SelectionStyle.SelectionBackColor), new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height - 2));
41+
42+
RectangleF bounds = new RectangleF(e.Bounds.X + 2, e.Bounds.Y + 2, e.Bounds.Width, e.Bounds.Height);
43+
44+
// Draw checkbox if enabled
45+
if (sfComboBox1.ComboBoxMode == ComboBoxMode.MultiSelection && sfComboBox1.DropDownListView.ShowCheckBoxes)
46+
{
47+
(e.Bounds.X + 2, e.Bounds.Y + 2, e.Bounds.Width, e.Bounds.Height - 2),
48+
listView.CheckedItems.Contains(e.ItemData) ? CheckState.Checked : CheckState.Unchecked,
49+
listView.Style.CheckBoxStyle);
50+
bounds = new RectangleF(e.Bounds.X + 20, e.Bounds.Y + 2, e.Bounds.Width, e.Bounds.Height - 2);
51+
}
52+
53+
// Draw item text
54+
e.Graphics.DrawString((e.ItemData as Student).StudentName, this.Font, new SolidBrush(Color.FromArgb(255, 51, 51, 51)), bounds);
55+
56+
// Draw separator line
57+
e.Graphics.DrawLine(new Pen(Color.Gray, 1) { DashPattern = new[] { 2f, 2f } }, e.Bounds.Left, e.Bounds.Bottom - 1, e.Bounds.Right, e.Bounds.Bottom - 1);
58+
}
59+
}
60+
61+
int mouseHoverItemIndex = -1;
62+
63+
private void SfComboBox1_MouseMove(object sender, MouseEventArgs e)
64+
{
65+
var list = sender as SfListView;
66+
mouseHoverItemIndex = list.GetRowIndexAtPoint(e.Location);
67+
list.Invalidate();
68+
}
69+
70+
```
71+
## Referece
72+
For more details, refer to the official KB article: https://www.syncfusion.com/kb/11498/how-to-add-separator-line-in-winforms-sfcombobox-dropdown-iems
73+
74+
## Screenshot
75+
![Separator in SfComboBox](SfComboBox/SfComboBox/Image/Add%20Separator%20to%20ComboBox.png)
13.7 KB
Loading

0 commit comments

Comments
 (0)