From 57e749ad67b6fea70038b8df49aeff5ff9c5ada2 Mon Sep 17 00:00:00 2001 From: brianlagunas_cp Date: Wed, 31 Aug 2011 01:33:34 +0000 Subject: [PATCH] CheckListBox: fixed bug where CheckedItems would be null when setting the items source. --- .../Implementation/CheckListBox.cs | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBox.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBox.cs index 31a30636..38232f9f 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBox.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBox.cs @@ -64,7 +64,7 @@ namespace Microsoft.Windows.Controls protected virtual void OnCheckedItemChanged(object oldValue, object newValue) { - + } #endregion //CheckedItem @@ -115,28 +115,40 @@ namespace Microsoft.Windows.Controls #endregion //Events - void CheckListBox_Checked(object sender, RoutedEventArgs e) + #region Methods + + private void CheckListBox_Checked(object sender, RoutedEventArgs e) { - SetCheckedItem(e.OriginalSource); - CheckedItems.Add(CheckedItem); + var item = GetDataContextItem(e.OriginalSource); + SetCheckedItem(item); + CheckedItems.Add(item); OnCheckedChanged(); } - void CheckListBox_Unchecked(object sender, RoutedEventArgs e) + private void CheckListBox_Unchecked(object sender, RoutedEventArgs e) { - SetCheckedItem(e.OriginalSource); - CheckedItems.Remove(CheckedItem); + 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; - var selectedCheckListBoxItem = source as FrameworkElement; - if (selectedCheckListBoxItem != null) - CheckedItem = selectedCheckListBoxItem.DataContext; + CheckedItem = source; } private void OnCheckedChanged() @@ -149,5 +161,7 @@ namespace Microsoft.Windows.Controls if (Command != null) Command.Execute(CheckedItem); } + + #endregion //Methods } }