From 171f315ddabe231d77f8641c2424eb9b1f4bd202 Mon Sep 17 00:00:00 2001 From: brianlagunas_cp Date: Fri, 24 Jun 2011 21:09:00 +0000 Subject: [PATCH] CheckListBox: still working on it --- .../Implementation/CheckListBox.cs | 64 +++++------------- .../Implementation/CheckListBoxItem.cs | 67 +++++++++++++++++-- .../CheckListBox/Themes/Generic.xaml | 10 +-- 3 files changed, 85 insertions(+), 56 deletions(-) diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBox.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBox.cs index f170920e..d45ff2c3 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBox.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBox.cs @@ -19,11 +19,18 @@ namespace Microsoft.Windows.Controls { public class CheckListBox : MultiSelector { + private bool _surpressSelectionChanged; + static CheckListBox() { DefaultStyleKeyProperty.OverrideMetadata(typeof(CheckListBox), new FrameworkPropertyMetadata(typeof(CheckListBox))); } + public CheckListBox() + { + + } + #region Properties public static readonly DependencyProperty CheckedMemberPathProperty = DependencyProperty.Register("CheckedMemberPath", typeof(string), typeof(CheckListBox), new UIPropertyMetadata(null)); @@ -41,49 +48,6 @@ namespace Microsoft.Windows.Controls set { SetValue(CommandProperty, value); } } - //public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(CheckListBox), new UIPropertyMetadata(null, new PropertyChangedCallback(OnItemsSourceChanged), new CoerceValueCallback(OnCoerceItemsSource))); - - //private static object OnCoerceItemsSource(DependencyObject o, object value) - //{ - // CheckListBox checkListBox = o as CheckListBox; - // if (checkListBox != null) - // return checkListBox.OnCoerceItemsSource((IEnumerable)value); - // else - // return value; - //} - - //private static void OnItemsSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) - //{ - // CheckListBox checkListBox = o as CheckListBox; - // if (checkListBox != null) - // checkListBox.OnItemsSourceChanged((IEnumerable)e.OldValue, (IEnumerable)e.NewValue); - //} - - //protected virtual IEnumerable OnCoerceItemsSource(IEnumerable value) - //{ - // // TODO: Keep the proposed value within the desired range. - // return value; - //} - - //protected virtual void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue) - //{ - // // TODO: Add your property changed side-effects. Descendants can override as well. - //} - - //public IEnumerable ItemsSource - //{ - // // IMPORTANT: To maintain parity between setting a property in XAML and procedural code, do not touch the getter and setter inside this dependency property! - // get - // { - // return (IEnumerable)GetValue(ItemsSourceProperty); - // } - // set - // { - // SetValue(ItemsSourceProperty, value); - // } - //} - - #endregion //Properties #region Base Class Overrides @@ -100,16 +64,24 @@ namespace Microsoft.Windows.Controls protected override void PrepareContainerForItemOverride(DependencyObject element, object item) { - var checkListBoxItem = element as FrameworkElement; + _surpressSelectionChanged = true; + var checkListBoxItem = element as FrameworkElement; if (!String.IsNullOrEmpty(CheckedMemberPath)) { Binding isCheckedBinding = new Binding(CheckedMemberPath); isCheckedBinding.Source = item; - checkListBoxItem.SetBinding(CheckListBoxItem.IsCheckedProperty, isCheckedBinding); + checkListBoxItem.SetBinding(CheckListBoxItem.IsSelectedProperty, isCheckedBinding); } - base.PrepareContainerForItemOverride(element, item); + + _surpressSelectionChanged = false; + } + + protected override void OnSelectionChanged(SelectionChangedEventArgs e) + { + if (!_surpressSelectionChanged) + base.OnSelectionChanged(e); } #endregion //Base Class Overrides diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxItem.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxItem.cs index 62057380..676b3c81 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxItem.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxItem.cs @@ -11,6 +11,7 @@ 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 { @@ -21,11 +22,67 @@ namespace Microsoft.Windows.Controls DefaultStyleKeyProperty.OverrideMetadata(typeof(CheckListBoxItem), new FrameworkPropertyMetadata(typeof(CheckListBoxItem))); } - public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool), typeof(CheckListBoxItem), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); - public bool IsChecked + #region Properties + + public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(CheckListBoxItem), new UIPropertyMetadata(false, OnIsSelectedChanged)); + public bool IsSelected + { + get { return (bool)GetValue(IsSelectedProperty); } + set { SetValue(IsSelectedProperty, 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) + OnSelected(new RoutedEventArgs(Selector.SelectedEvent, this)); + else + OnUnselected(new RoutedEventArgs(Selector.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); } + } + + #endregion + + #region Methods + + protected virtual void OnSelected(RoutedEventArgs e) + { + this.OnIsSelectedChanged(true, e); + } + + protected virtual void OnUnselected(RoutedEventArgs e) { - get { return (bool)GetValue(IsCheckedProperty); } - set { SetValue(IsCheckedProperty, value); } - } + this.OnIsSelectedChanged(false, e); + } + + private void OnIsSelectedChanged(bool newValue, 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 25eef411..741f5b29 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Themes/Generic.xaml +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Themes/Generic.xaml @@ -34,7 +34,7 @@ - @@ -43,16 +43,16 @@ - - + - + -->