| using System; |
| using System.Collections.ObjectModel; |
| using System.ComponentModel; |
| using System.Text; |
| using System.Windows.Controls; |
| |
| namespace SilverlightDockingDemo |
| { |
| public partial class Page48 : UserControl |
| { |
| public Page48() |
| { |
| InitializeComponent(); |
| } |
| } |
| |
| public class MyDataItem : INotifyPropertyChanged |
| { |
| #region INotifyPropertyChanged Members |
| |
| /// <summary> |
| /// Called when the value of a property changes. |
| /// </summary> |
| /// <param name="propertyName">The name of the property that has changed.</param> |
| protected virtual void OnPropertyChanged(String propertyName) |
| { |
| if (String.IsNullOrEmpty(propertyName)) |
| { |
| return; |
| } |
| if (PropertyChanged != null) |
| { |
| PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); |
| } |
| } |
| |
| /// <summary> |
| /// Raised when the value of one of the properties changes. |
| /// </summary> |
| public event PropertyChangedEventHandler PropertyChanged; |
|
| #endregion |
| |
| #region Text |
| |
| private string text; |
| /// <summary> |
| /// Gets or sets Text. |
| /// </summary> |
| public string Text |
| { |
| get |
| { |
| return this.text; |
| } |
| set |
| { |
| if (this.text != value) |
| { |
| this.text = value; |
| OnPropertyChanged("Text"); |
| } |
| } |
| } |
|
| #endregion |
| |
| #region IsSelected |
| |
| private bool selected; |
| /// <summary> |
| /// Gets or sets IsSelected. |
| /// </summary> |
| public bool IsSelected |
| { |
| get |
| { |
| return this.selected; |
| } |
| set |
| { |
| if (this.selected != value) |
| { |
| this.selected = value; |
| OnPropertyChanged("IsSelected"); |
| } |
| } |
| } |
|
| #endregion |
| } |
| |
| public class ComboBoxSource : ObservableCollection<MyDataItem> |
| { |
| #region SelectedItemsText |
| |
| private string text; |
| /// <summary> |
| /// Gets or sets SelectedItemsText. |
| /// </summary> |
| public string SelectedItemsText |
| { |
| get |
| { |
| return this.text; |
| } |
| set |
| { |
| if (this.text != value) |
| { |
| this.text = value; |
| this.OnPropertyChanged(new PropertyChangedEventArgs("SelectedItemsText")); |
| } |
| } |
| } |
|
| #endregion |
| |
| protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e) |
| { |
| if (e.NewItems != null) |
| { |
| foreach (MyDataItem item in e.NewItems) |
| { |
| item.PropertyChanged += new PropertyChangedEventHandler(OnItemPropertyChanged); |
| } |
| } |
| if (e.OldItems != null) |
| { |
| foreach (MyDataItem item in e.OldItems) |
| { |
| item.PropertyChanged -= new PropertyChangedEventHandler(OnItemPropertyChanged); |
| } |
| } |
| base.OnCollectionChanged(e); |
| this.UpdateSelectedText(); |
| } |
| |
| public void UpdateSelectedText() |
| { |
| int itemsCount = this.Items.Count; |
| StringBuilder sb = new StringBuilder(); |
| for (int i = 0; i < itemsCount; i++) |
| { |
| MyDataItem item = this.Items[i]; |
| if (item.IsSelected) |
| { |
| sb.AppendFormat("{0}, ", item.Text); |
| } |
| } |
| |
| if (sb.Length > 2) |
| { |
| sb.Remove(sb.Length - 2, 2); |
| } |
| |
| this.SelectedItemsText = sb.ToString(); |
| } |
| |
| private void OnItemPropertyChanged(object sender, PropertyChangedEventArgs e) |
| { |
| if (e.PropertyName == "IsSelected") |
| { |
| this.UpdateSelectedText(); |
| } |
| } |
| } |
| } |