Browse Source

CheckListBox: fixed bug where CheckedItems would be null when setting the items source.

pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
57e749ad67
  1. 34
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBox.cs

34
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBox.cs

@ -64,7 +64,7 @@ namespace Microsoft.Windows.Controls
protected virtual void OnCheckedItemChanged(object oldValue, object newValue) protected virtual void OnCheckedItemChanged(object oldValue, object newValue)
{ {
} }
#endregion //CheckedItem #endregion //CheckedItem
@ -115,28 +115,40 @@ namespace Microsoft.Windows.Controls
#endregion //Events #endregion //Events
void CheckListBox_Checked(object sender, RoutedEventArgs e) #region Methods
private void CheckListBox_Checked(object sender, RoutedEventArgs e)
{ {
SetCheckedItem(e.OriginalSource); var item = GetDataContextItem(e.OriginalSource);
CheckedItems.Add(CheckedItem); SetCheckedItem(item);
CheckedItems.Add(item);
OnCheckedChanged(); OnCheckedChanged();
} }
void CheckListBox_Unchecked(object sender, RoutedEventArgs e) private void CheckListBox_Unchecked(object sender, RoutedEventArgs e)
{ {
SetCheckedItem(e.OriginalSource); var item = GetDataContextItem(e.OriginalSource);
CheckedItems.Remove(CheckedItem); SetCheckedItem(item);
CheckedItems.Remove(item);
OnCheckedChanged(); 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) private void SetCheckedItem(object source)
{ {
if (_surpressSelectionChanged) if (_surpressSelectionChanged)
return; return;
var selectedCheckListBoxItem = source as FrameworkElement; CheckedItem = source;
if (selectedCheckListBoxItem != null)
CheckedItem = selectedCheckListBoxItem.DataContext;
} }
private void OnCheckedChanged() private void OnCheckedChanged()
@ -149,5 +161,7 @@ namespace Microsoft.Windows.Controls
if (Command != null) if (Command != null)
Command.Execute(CheckedItem); Command.Execute(CheckedItem);
} }
#endregion //Methods
} }
} }

Loading…
Cancel
Save