summaryrefslogtreecommitdiff
path: root/src/app/Cmpp298.Assignment3.Desktop.Adapters/DesktopDropDownList.cs
blob: 1f3a2ee6b50658f79c1b1fb748f3873f173d7e5f (plain)
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
/*
 * Created by: Mo
 * Created: Sunday, August 12, 2007
 */

using System.Collections.Generic;
using System.Windows.Forms;
using Cmpp298.Assignment3.Dto;

namespace Cmpp298.Assignment3.Desktop.Adapters {
	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; }
		}

		public void SetSelectedItemTo( string text ) {
			_dropDown.SelectedIndex = _dropDown.Items.IndexOf( text );
		}

		private ComboBox _dropDown;
		private IDictionary< string, IDropDownListItem > _pairs;
	}
}