using System.Collections.Generic; using System.Windows.Forms; namespace DesignPatterns.Adapter { public class DesktopDropDownList : IDropDownListAdapter { public DesktopDropDownList( ComboBox dropDown ) { _dropDown = dropDown; _pairs = new Dictionary< string, IDropDownListItem >( ); } public void BindTo( IEnumerable< IDropDownListItem > pairs ) { if ( pairs != null ) { _pairs = new Dictionary< string, IDropDownListItem >( ); foreach ( IDropDownListItem pair in pairs ) { _dropDown.Items.Add( pair.Text ); _pairs.Add( pair.Text, pair ); } _dropDown.SelectedIndex = 0; } } public IDropDownListItem SelectedItem { get { return !string.IsNullOrEmpty( _dropDown.Text ) ? _pairs[ _dropDown.Text ] : null; } } private ComboBox _dropDown; private IDictionary< string, IDropDownListItem > _pairs; } }