ComboBox TestCode
No notes
Syntax:
C#
// -------------------------------------------------------------------------------------------------------------------- // <copyright file="ComboBoxExample.xaml.cs" company="Morgan Stanley"> // // </copyright> // <summary> // The paste. // </summary> // -------------------------------------------------------------------------------------------------------------------- namespace WMSManager { using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Windows; using System.Windows.Controls; using System.Windows.Navigation; /// <summary> /// The paste. /// </summary> public partial class ComboBoxExample : Page { #region Constructors and Destructors /// <summary> /// Initializes a new instance of the <see cref="ComboBoxExample"/> class. /// </summary> public ComboBoxExample() { this.InitializeComponent(); this.Loaded += this.OnLoaded; this.MyComboBox.SelectionChanged += this.MyComboBoxSelectionChanged; } #endregion // Executes when the user navigates to this page. #region Methods /// <summary> /// The on navigated to. /// </summary> /// <param name="e"> /// The e. /// </param> protected override void OnNavigatedTo(NavigationEventArgs e) { } /// <summary> /// The get my items. /// </summary> /// <returns> /// </returns> private static ObservableCollection<MyItem> GetMyItems() { var myitems = new MyItemCollection(); for (int i = 1; i <= 5; i++) { myitems.Add(new MyItem(i.ToString(), "MyItem" + i)); } return myitems.ToObservableCollection(); } /// <summary> /// The bind my items. /// </summary> private void BindMyItems() { ObservableCollection<MyItem> myitems = GetMyItems(); this.MyComboBox.DataContext = myitems; this.MyComboBox.SelectedItem = myitems[0]; } /// <summary> /// The my combo box selection changed. /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="e"> /// The e. /// </param> private void MyComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e) { var c = ((ComboBox)sender).SelectedItem as MyItem; if (c != null) { // DoSomething } } /// <summary> /// The on loaded. /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="e"> /// The e. /// </param> private void OnLoaded(object sender, RoutedEventArgs e) { this.BindMyItems(); } #endregion } /// <summary> /// The extensions. /// </summary> public static class Extensions { #region Public Methods /// <summary> /// The to observable collection. /// </summary> /// <param name="enumerableList"> /// The enumerable list. /// </param> /// <typeparam name="T"> /// </typeparam> /// <returns> /// </returns> public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> enumerableList) { if (enumerableList != null) { // create an emtpy observable collection object var observableCollection = new ObservableCollection<T>(); // loop through all the records and add to observable collection object foreach (T item in enumerableList) { observableCollection.Add(item); } // return the populated observable collection return observableCollection; } return null; } #endregion } /// <summary> /// The my item. /// </summary> public class MyItem { #region Constructors and Destructors /// <summary> /// Initializes a new instance of the <see cref="MyItem"/> class. /// </summary> /// <param name="myitemid"> /// The myitemid. /// </param> /// <param name="myitemname"> /// The myitemname. /// </param> public MyItem(string myitemid, string myitemname) { this.MyItemId = myitemid; this.MyItemName = myitemname; } #endregion #region Properties /// <summary> /// Gets or sets MyItemId. /// </summary> public string MyItemId { get; set; } /// <summary> /// Gets or sets MyItemName. /// </summary> public string MyItemName { get; set; } #endregion } /// <summary> /// The my item collection. /// </summary> public class MyItemCollection : IEnumerable<MyItem> { #region Constants and Fields /// <summary> /// The _myitems. /// </summary> private readonly List<MyItem> _myitems = new List<MyItem>(); #endregion #region Public Methods /// <summary> /// The add. /// </summary> /// <param name="myitem"> /// The myitem. /// </param> public void Add(MyItem myitem) { this._myitems.Add(myitem); } /// <summary> /// The add range. /// </summary> /// <param name="myitem"> /// The myitem. /// </param> public void AddRange(IEnumerable<MyItem> myitem) { this._myitems.AddRange(myitem); } #endregion #region Implemented Interfaces #region IEnumerable /// <summary> /// The get enumerator. /// </summary> /// <returns> /// </returns> IEnumerator IEnumerable.GetEnumerator() { return ((IEnumerable)this._myitems).GetEnumerator(); } #endregion #region IEnumerable<MyItem> /// <summary> /// The get enumerator. /// </summary> /// <returns> /// </returns> public IEnumerator<MyItem> GetEnumerator() { return this._myitems.GetEnumerator(); } #endregion #endregion } }