diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBox.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBox.cs index 38232f9f..9b74466e 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBox.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBox.cs @@ -1,18 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Windows; -using System.Windows.Controls; -using System.Windows.Data; -using System.Windows.Input; -using System.ComponentModel; -using System.Collections; +using System.Windows; +using Microsoft.Windows.Controls.Primitives; namespace Microsoft.Windows.Controls { - public class CheckListBox : ItemsControl + public class CheckListBox : Selector { - private bool _surpressSelectionChanged; - #region Constructors static CheckListBox() @@ -22,146 +14,9 @@ namespace Microsoft.Windows.Controls public CheckListBox() { - CheckedItems = new List(); - AddHandler(CheckListBox.CheckedEvent, new RoutedEventHandler(CheckListBox_Checked)); - AddHandler(CheckListBox.UncheckedEvent, new RoutedEventHandler(CheckListBox_Unchecked)); - } - - #endregion //Constructors - - #region Properties - - public static readonly DependencyProperty CheckedMemberPathProperty = DependencyProperty.Register("CheckedMemberPath", typeof(string), typeof(CheckListBox), new UIPropertyMetadata(null)); - public string CheckedMemberPath - { - get { return (string)GetValue(CheckedMemberPathProperty); } - set { SetValue(CheckedMemberPathProperty, value); } - } - - public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(CheckListBox), new PropertyMetadata((ICommand)null)); - [TypeConverter(typeof(CommandConverter))] - public ICommand Command - { - get { return (ICommand)GetValue(CommandProperty); } - set { SetValue(CommandProperty, value); } - } - - #region CheckedItem - - public static readonly DependencyProperty CheckedItemProperty = DependencyProperty.Register("CheckedItem", typeof(object), typeof(CheckListBox), new UIPropertyMetadata(null, OnCheckedItemChanged)); - public object CheckedItem - { - get { return (object)GetValue(CheckedItemProperty); } - set { SetValue(CheckedItemProperty, value); } - } - - private static void OnCheckedItemChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) - { - CheckListBox checkListBox = o as CheckListBox; - if (checkListBox != null) - checkListBox.OnCheckedItemChanged((object)e.OldValue, (object)e.NewValue); - } - - protected virtual void OnCheckedItemChanged(object oldValue, object newValue) - { - - } - - #endregion //CheckedItem - - public IList CheckedItems { get; private set; } - - #endregion //Properties - - #region Base Class Overrides - - protected override DependencyObject GetContainerForItemOverride() - { - return new CheckListBoxItem(); - } - - protected override bool IsItemItsOwnContainerOverride(object item) - { - return item is CheckListBoxItem; - } - - protected override void PrepareContainerForItemOverride(DependencyObject element, object item) - { - _surpressSelectionChanged = true; - var checkListBoxItem = element as FrameworkElement; - if (!String.IsNullOrEmpty(CheckedMemberPath)) - { - Binding isCheckedBinding = new Binding(CheckedMemberPath); - isCheckedBinding.Mode = BindingMode.TwoWay; - isCheckedBinding.Source = item; - checkListBoxItem.SetBinding(CheckListBoxItem.IsCheckedProperty, isCheckedBinding); - } - base.PrepareContainerForItemOverride(element, item); - _surpressSelectionChanged = false; - } - - #endregion //Base Class Overrides - - #region Events - - public static readonly RoutedEvent CheckedEvent = EventManager.RegisterRoutedEvent("CheckedEvent", RoutingStrategy.Bubble, typeof(SelectionChangedEventHandler), typeof(CheckListBox)); - public static readonly RoutedEvent UncheckedEvent = EventManager.RegisterRoutedEvent("UncheckedEvent", RoutingStrategy.Bubble, typeof(SelectionChangedEventHandler), typeof(CheckListBox)); - public static readonly RoutedEvent CheckedChangedEvent = EventManager.RegisterRoutedEvent("CheckedChanged", RoutingStrategy.Bubble, typeof(CheckListBoxCheckedChangedEventHandler), typeof(CheckListBox)); - public event CheckListBoxCheckedChangedEventHandler CheckedChanged - { - add { AddHandler(CheckedChangedEvent, value); } - remove { RemoveHandler(CheckedChangedEvent, value); } - } - - #endregion //Events - - #region Methods - - private void CheckListBox_Checked(object sender, RoutedEventArgs e) - { - var item = GetDataContextItem(e.OriginalSource); - SetCheckedItem(item); - CheckedItems.Add(item); - OnCheckedChanged(); - } - - private void CheckListBox_Unchecked(object sender, RoutedEventArgs e) - { - var item = GetDataContextItem(e.OriginalSource); - SetCheckedItem(item); - CheckedItems.Remove(item); - OnCheckedChanged(); - } - - private object GetDataContextItem(object source) - { - var selectedCheckListBoxItem = source as FrameworkElement; - if (selectedCheckListBoxItem != null) - return selectedCheckListBoxItem.DataContext; - else - return null; } - private void SetCheckedItem(object source) - { - if (_surpressSelectionChanged) - return; - - CheckedItem = source; - } - - private void OnCheckedChanged() - { - if (_surpressSelectionChanged) - return; - - RaiseEvent(new CheckListBoxCheckedChangedEventArgs(CheckListBox.CheckedChangedEvent, this, CheckedItem)); - - if (Command != null) - Command.Execute(CheckedItem); - } - - #endregion //Methods + #endregion //Constructors } } diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxCheckedChangedEventArgs.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxCheckedChangedEventArgs.cs deleted file mode 100644 index 7450379c..00000000 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxCheckedChangedEventArgs.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Windows; - -namespace Microsoft.Windows.Controls -{ - public delegate void CheckListBoxCheckedChangedEventHandler(object sender, CheckListBoxCheckedChangedEventArgs e); - public class CheckListBoxCheckedChangedEventArgs : RoutedEventArgs - { - public object Item { get; private set; } - - public CheckListBoxCheckedChangedEventArgs(RoutedEvent routedEvent, object source, object item) - : base(routedEvent, source) - { - Item = item; - } - } -} diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxItem.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxItem.cs deleted file mode 100644 index 1993c7ca..00000000 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxItem.cs +++ /dev/null @@ -1,81 +0,0 @@ -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; - -namespace Microsoft.Windows.Controls -{ - public class CheckListBoxItem : ContentControl - { - static CheckListBoxItem() - { - DefaultStyleKeyProperty.OverrideMetadata(typeof(CheckListBoxItem), new FrameworkPropertyMetadata(typeof(CheckListBoxItem))); - } - - public CheckListBoxItem() - { - AddHandler(Mouse.MouseDownEvent, new MouseButtonEventHandler(CheckListBoxItem_MouseDown)); - } - - #region Properties - - public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool), typeof(CheckListBoxItem), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsSelectedChanged)); - public bool IsChecked - { - get { return (bool)GetValue(IsCheckedProperty); } - set { SetValue(IsCheckedProperty, value); } - } - - private static void OnIsSelectedChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) - { - CheckListBoxItem checkListBoxItem = o as CheckListBoxItem; - if (checkListBoxItem != null) - checkListBoxItem.OnIsSelectedChanged((bool)e.OldValue, (bool)e.NewValue); - } - - protected virtual void OnIsSelectedChanged(bool oldValue, bool newValue) - { - if (newValue) - RaiseSelectionChangedEvent(new RoutedEventArgs(CheckListBox.CheckedEvent, this)); - else - RaiseSelectionChangedEvent(new RoutedEventArgs(CheckListBox.UncheckedEvent, this)); - } - - #endregion //Properties - - #region Events - - public static readonly RoutedEvent SelectedEvent = CheckListBox.CheckedEvent.AddOwner(typeof(CheckListBoxItem)); - public static readonly RoutedEvent UnselectedEvent = CheckListBox.UncheckedEvent.AddOwner(typeof(CheckListBoxItem)); - - #endregion - - #region Event Hanlders - - void CheckListBoxItem_MouseDown(object sender, MouseButtonEventArgs e) - { - IsChecked = !IsChecked; - } - - #endregion //Event Hanlders - - #region Methods - - private void RaiseSelectionChangedEvent(RoutedEventArgs e) - { - base.RaiseEvent(e); - } - - #endregion //Methods - } -} diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Themes/Generic.xaml b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Themes/Generic.xaml index aa0c4359..7706806b 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Themes/Generic.xaml +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Themes/Generic.xaml @@ -23,36 +23,4 @@ - - \ No newline at end of file diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Primitives/Selector.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Primitives/Selector.cs index ff800dcb..80c1326a 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Primitives/Selector.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Primitives/Selector.cs @@ -1,16 +1,15 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Windows.Controls; -using System.Windows; -using System.Windows.Data; using System.Collections; using System.Collections.ObjectModel; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.ComponentModel; +using System.Windows.Input; namespace Microsoft.Windows.Controls.Primitives { - public class Selector : ItemsControl + public class Selector : ItemsControl //should probably make this control an ICommandSource { #region Members @@ -25,13 +24,21 @@ namespace Microsoft.Windows.Controls.Primitives { SelectedItems = new ObservableCollection(); AddHandler(Selector.SelectedEvent, new RoutedEventHandler(Selector_ItemSelected)); - AddHandler(Selector.UnSelectedEvent, new RoutedEventHandler(Selector_ItemUnSelected)); + AddHandler(Selector.UnSelectedEvent, new RoutedEventHandler(Selector_ItemUnselected)); } #endregion //Constructors #region Properties + public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(CheckListBox), new PropertyMetadata((ICommand)null)); + [TypeConverter(typeof(CommandConverter))] + public ICommand Command + { + get { return (ICommand)GetValue(CommandProperty); } + set { SetValue(CommandProperty, value); } + } + public static readonly DependencyProperty DelimiterProperty = DependencyProperty.Register("Delimiter", typeof(string), typeof(Selector), new UIPropertyMetadata(",")); public string Delimiter { @@ -189,14 +196,14 @@ namespace Microsoft.Windows.Controls.Primitives #region Events - public static readonly RoutedEvent SelectedEvent = EventManager.RegisterRoutedEvent("SelectedEvent", RoutingStrategy.Bubble, typeof(SelectionChangedEventHandler), typeof(Selector)); - public static readonly RoutedEvent UnSelectedEvent = EventManager.RegisterRoutedEvent("UnSelectedEvent", RoutingStrategy.Bubble, typeof(SelectionChangedEventHandler), typeof(Selector)); + public static readonly RoutedEvent SelectedEvent = EventManager.RegisterRoutedEvent("SelectedEvent", RoutingStrategy.Bubble, typeof(SelectedItemChangedEventHandler), typeof(Selector)); + public static readonly RoutedEvent UnSelectedEvent = EventManager.RegisterRoutedEvent("UnSelectedEvent", RoutingStrategy.Bubble, typeof(SelectedItemChangedEventHandler), typeof(Selector)); - public static readonly RoutedEvent SelectionChangedEvent = EventManager.RegisterRoutedEvent("SelectionChanged", RoutingStrategy.Bubble, typeof(SelectionChangedEventHandler), typeof(Selector)); - public event SelectionChangedEventHandler SelectionChanged + public static readonly RoutedEvent SelectedItemChangedEvent = EventManager.RegisterRoutedEvent("SelectedItemChanged", RoutingStrategy.Bubble, typeof(SelectedItemChangedEventHandler), typeof(Selector)); + public event SelectedItemChangedEventHandler SelectionItemChanged { - add { AddHandler(SelectionChangedEvent, value); } - remove { RemoveHandler(SelectionChangedEvent, value); } + add { AddHandler(SelectedItemChangedEvent, value); } + remove { RemoveHandler(SelectedItemChangedEvent, value); } } #endregion //Events @@ -208,7 +215,7 @@ namespace Microsoft.Windows.Controls.Primitives OnItemSelected(e.OriginalSource, false); } - protected virtual void Selector_ItemUnSelected(object sender, RoutedEventArgs e) + protected virtual void Selector_ItemUnselected(object sender, RoutedEventArgs e) { OnItemSelected(e.OriginalSource, true); } @@ -248,19 +255,18 @@ namespace Microsoft.Windows.Controls.Primitives { var item = GetDataContextItem(source); Update(item, remove); - - if (remove) - OnSelectionChanged(new List() { item }, new List()); - else - OnSelectionChanged(new List(), new List() { item }); + RaiseSelectionItemChangedEvent(item); } - private void OnSelectionChanged(IList removedItems, IList addedItems) + protected virtual void RaiseSelectionItemChangedEvent(object item) { if (_surpressSelectionChanged) return; - RaiseEvent(new SelectionChangedEventArgs(Selector.SelectionChangedEvent, removedItems, addedItems)); + RaiseEvent(new SelectedItemChangedEventArgs(Selector.SelectedItemChangedEvent, this, item)); + + if (Command != null) + Command.Execute(SelectedItem); } protected virtual void Update(object item, bool remove) @@ -371,4 +377,16 @@ namespace Microsoft.Windows.Controls.Primitives #endregion //Methods } + + public delegate void SelectedItemChangedEventHandler(object sender, SelectedItemChangedEventArgs e); + public class SelectedItemChangedEventArgs : RoutedEventArgs + { + public object Item { get; private set; } + + public SelectedItemChangedEventArgs(RoutedEvent routedEvent, object source, object item) + : base(routedEvent, source) + { + Item = item; + } + } } \ No newline at end of file diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj index 3acb4d06..50f2d972 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj @@ -182,9 +182,7 @@ - -