From 096fd815cedab92fe013cb44d5f178368cd24808 Mon Sep 17 00:00:00 2001 From: brianlagunas_cp Date: Fri, 24 Jun 2011 22:37:45 +0000 Subject: [PATCH] working on the CheckListBox. You bind your collection to the ItemsSource property. Set the DisplayMemberPath, and the CheckedMemberPath. It also supports Commanding. Set the Command property to your command. The parameter will default to the SelectedItem, which is the item that was just checked/unchecked. It also has a SelectionChganged event. You can obtain the object the was check/unchecked from the e.Item property of the event args. --- .../Implementation/CheckListBox.cs | 93 +++++++++++++++---- .../Implementation/CheckListBoxItem.cs | 33 ++----- .../CheckListBoxSelectionChangedEventArgs.cs | 17 ++++ .../CheckListBox/Themes/Generic.xaml | 5 +- .../WPFToolkit.Extended.csproj | 1 + 5 files changed, 103 insertions(+), 46 deletions(-) create mode 100644 ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxSelectionChangedEventArgs.cs diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBox.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBox.cs index d45ff2c3..32f0af31 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBox.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBox.cs @@ -1,26 +1,20 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; -using System.Windows.Documents; using System.Windows.Input; -using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Navigation; -using System.Windows.Shapes; -using System.Windows.Controls.Primitives; using System.ComponentModel; using System.Collections; namespace Microsoft.Windows.Controls { - public class CheckListBox : MultiSelector + public class CheckListBox : ItemsControl { private bool _surpressSelectionChanged; + #region Constructors + static CheckListBox() { DefaultStyleKeyProperty.OverrideMetadata(typeof(CheckListBox), new FrameworkPropertyMetadata(typeof(CheckListBox))); @@ -28,9 +22,13 @@ namespace Microsoft.Windows.Controls public CheckListBox() { - + SelectedItems = new List(); + AddHandler(CheckListBox.SelectedEvent, new RoutedEventHandler(CheckListBox_Selected)); + AddHandler(CheckListBox.UnselectedEvent, new RoutedEventHandler(CheckListBox_Unselected)); } + #endregion //Constructors + #region Properties public static readonly DependencyProperty CheckedMemberPathProperty = DependencyProperty.Register("CheckedMemberPath", typeof(string), typeof(CheckListBox), new UIPropertyMetadata(null)); @@ -48,6 +46,31 @@ namespace Microsoft.Windows.Controls set { SetValue(CommandProperty, value); } } + #region SelectedItem + + public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(CheckListBox), new UIPropertyMetadata(null, OnSelectedItemChanged)); + public object SelectedItem + { + get { return (object)GetValue(SelectedItemProperty); } + set { SetValue(SelectedItemProperty, value); } + } + + private static void OnSelectedItemChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) + { + CheckListBox checkListBox = o as CheckListBox; + if (checkListBox != null) + checkListBox.OnSelectedItemChanged((object)e.OldValue, (object)e.NewValue); + } + + protected virtual void OnSelectedItemChanged(object oldValue, object newValue) + { + OnSelectionChanged(); + } + + #endregion //SelectedItem + + public IList SelectedItems { get; private set; } + #endregion //Properties #region Base Class Overrides @@ -65,7 +88,6 @@ namespace Microsoft.Windows.Controls protected override void PrepareContainerForItemOverride(DependencyObject element, object item) { _surpressSelectionChanged = true; - var checkListBoxItem = element as FrameworkElement; if (!String.IsNullOrEmpty(CheckedMemberPath)) { @@ -74,16 +96,55 @@ namespace Microsoft.Windows.Controls checkListBoxItem.SetBinding(CheckListBoxItem.IsSelectedProperty, isCheckedBinding); } base.PrepareContainerForItemOverride(element, item); - _surpressSelectionChanged = false; } - protected override void OnSelectionChanged(SelectionChangedEventArgs e) + #endregion //Base Class Overrides + + #region Events + + public static readonly RoutedEvent SelectedEvent = EventManager.RegisterRoutedEvent("Selected", RoutingStrategy.Bubble, typeof(SelectionChangedEventHandler), typeof(CheckListBox)); + public static readonly RoutedEvent UnselectedEvent = EventManager.RegisterRoutedEvent("Unselected", RoutingStrategy.Bubble, typeof(SelectionChangedEventHandler), typeof(CheckListBox)); + public static readonly RoutedEvent SelectionChangedEvent = EventManager.RegisterRoutedEvent("SelectionChanged", RoutingStrategy.Bubble, typeof(CheckListBoxSelectionChangedEventHandler), typeof(CheckListBox)); + public event CheckListBoxSelectionChangedEventHandler SelectionChanged + { + add { AddHandler(SelectionChangedEvent, value); } + remove { RemoveHandler(SelectionChangedEvent, value); } + } + + #endregion //Events + + void CheckListBox_Selected(object sender, RoutedEventArgs e) { - if (!_surpressSelectionChanged) - base.OnSelectionChanged(e); + SetSelectedItem(e.OriginalSource); + SelectedItems.Add(SelectedItem); } - #endregion //Base Class Overrides + void CheckListBox_Unselected(object sender, RoutedEventArgs e) + { + SetSelectedItem(e.OriginalSource); + SelectedItems.Remove(SelectedItem); + } + + private void SetSelectedItem(object source) + { + if (_surpressSelectionChanged) + return; + + var selectedCheckListBoxItem = source as FrameworkElement; + if (selectedCheckListBoxItem != null) + SelectedItem = selectedCheckListBoxItem.DataContext; + } + + private void OnSelectionChanged() + { + if (_surpressSelectionChanged) + return; + + RaiseEvent(new CheckListBoxSelectionChangedEventArgs(CheckListBox.SelectionChangedEvent, this, SelectedItem)); + + if (Command != null) + Command.Execute(SelectedItem); + } } } diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxItem.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxItem.cs index 676b3c81..17649cee 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxItem.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxItem.cs @@ -24,7 +24,7 @@ namespace Microsoft.Windows.Controls #region Properties - public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(CheckListBoxItem), new UIPropertyMetadata(false, OnIsSelectedChanged)); + public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(CheckListBoxItem), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsSelectedChanged)); public bool IsSelected { get { return (bool)GetValue(IsSelectedProperty); } @@ -41,44 +41,23 @@ namespace Microsoft.Windows.Controls protected virtual void OnIsSelectedChanged(bool oldValue, bool newValue) { if (newValue) - OnSelected(new RoutedEventArgs(Selector.SelectedEvent, this)); + RaiseSelectionChangedEvent(new RoutedEventArgs(CheckListBox.SelectedEvent, this)); else - OnUnselected(new RoutedEventArgs(Selector.UnselectedEvent, this)); + RaiseSelectionChangedEvent(new RoutedEventArgs(CheckListBox.UnselectedEvent, this)); } #endregion //Properties #region Events - public static readonly RoutedEvent SelectedEvent = Selector.SelectedEvent.AddOwner(typeof(CheckListBoxItem)); - public event RoutedEventHandler Selected - { - add { base.AddHandler(SelectedEvent, value); } - remove { base.RemoveHandler(SelectedEvent, value); } - } - - public static readonly RoutedEvent UnselectedEvent = Selector.UnselectedEvent.AddOwner(typeof(CheckListBoxItem)); - public event RoutedEventHandler Unselected - { - add { base.AddHandler(UnselectedEvent, value); } - remove { base.RemoveHandler(UnselectedEvent, value); } - } + public static readonly RoutedEvent SelectedEvent = CheckListBox.SelectedEvent.AddOwner(typeof(CheckListBoxItem)); + public static readonly RoutedEvent UnselectedEvent = CheckListBox.UnselectedEvent.AddOwner(typeof(CheckListBoxItem)); #endregion #region Methods - protected virtual void OnSelected(RoutedEventArgs e) - { - this.OnIsSelectedChanged(true, e); - } - - protected virtual void OnUnselected(RoutedEventArgs e) - { - this.OnIsSelectedChanged(false, e); - } - - private void OnIsSelectedChanged(bool newValue, RoutedEventArgs e) + private void RaiseSelectionChangedEvent(RoutedEventArgs e) { base.RaiseEvent(e); } diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxSelectionChangedEventArgs.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxSelectionChangedEventArgs.cs new file mode 100644 index 00000000..9db590cd --- /dev/null +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxSelectionChangedEventArgs.cs @@ -0,0 +1,17 @@ +using System; +using System.Windows; + +namespace Microsoft.Windows.Controls +{ + public delegate void CheckListBoxSelectionChangedEventHandler(object sender, CheckListBoxSelectionChangedEventArgs e); + public class CheckListBoxSelectionChangedEventArgs : RoutedEventArgs + { + public object Item { get; private set; } + + public CheckListBoxSelectionChangedEventArgs(RoutedEvent routedEvent, object source, object item) + : base(routedEvent, source) + { + Item = item; + } + } +} diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Themes/Generic.xaml b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Themes/Generic.xaml index 741f5b29..4714eca7 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Themes/Generic.xaml +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Themes/Generic.xaml @@ -34,9 +34,8 @@ - + diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj index 4db37758..26ac1d6d 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj @@ -152,6 +152,7 @@ +