-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComboCascader.cs
49 lines (42 loc) · 1.03 KB
/
ComboCascader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace CascadingComboSpike
{
public class ComboCascader
{
public ComboBox Self { get; private set; }
public ComboBox Parent { get; private set; }
private readonly Action _selfPopulater;
public ComboCascader(ComboBox parent, ComboBox self, Action selfPopulater)
{
Parent = parent;
Self = self;
_selfPopulater = selfPopulater;
DoTheCascade();
Parent.SelectedValueChanged += (src, args) => DoTheCascade();
Parent.EnabledChanged += (src, args) => DoTheCascade();
}
private void DoTheCascade()
{
if (!Parent.Enabled)
{
Self.Enabled = false;
Self.Items.Clear();
return;
}
object previousValue = Self.SelectedItem;
_selfPopulater();
Self.Enabled = Self.Items.Count > 0;
if (previousValue != null && Self.Items.Contains(previousValue))
{
Self.SelectedItem = previousValue;
}
else
{
Self.SelectedItem = null;
}
}
}
}