Browse Source

stablized the CheckListBox and CheckComboBox

pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
a5d3633aac
  1. 24
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckComboBox/Implementation/CheckComboBox.cs
  2. 120
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Primitives/Selector.cs

24
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckComboBox/Implementation/CheckComboBox.cs

@ -6,8 +6,6 @@ namespace Microsoft.Windows.Controls
{ {
public class CheckComboBox : Selector public class CheckComboBox : Selector
{ {
private bool _surpressTextUpdateFromSelectedValueChanged;
#region Constructors #region Constructors
static CheckComboBox() static CheckComboBox()
@ -66,25 +64,7 @@ namespace Microsoft.Windows.Controls
protected override void OnSelectedValueChanged(string oldValue, string newValue) protected override void OnSelectedValueChanged(string oldValue, string newValue)
{ {
base.OnSelectedValueChanged(oldValue, newValue); base.OnSelectedValueChanged(oldValue, newValue);
UpdateTextFromSelectedValue();
if (!_surpressTextUpdateFromSelectedValueChanged)
UpdateTextFromSelectedValue();
}
protected override void OnSelectedItemsCollectionChanged(object item, bool remove)
{
_surpressTextUpdateFromSelectedValueChanged = true;
base.OnSelectedItemsCollectionChanged(item, remove);
UpdateDisplayText(item, remove);
_surpressTextUpdateFromSelectedValueChanged = false;
}
protected override void Update(object item, bool remove)
{
_surpressTextUpdateFromSelectedValueChanged = true;
base.Update(item, remove);
UpdateDisplayText(item, remove);
_surpressTextUpdateFromSelectedValueChanged = false;
} }
#endregion //Base Class Overrides #endregion //Base Class Overrides
@ -125,6 +105,8 @@ namespace Microsoft.Windows.Controls
private void UpdateTextFromSelectedValue() private void UpdateTextFromSelectedValue()
{ {
UpdateText(String.Empty);
if (!String.IsNullOrEmpty(SelectedValue)) if (!String.IsNullOrEmpty(SelectedValue))
{ {
string[] values = SelectedValue.Split(new string[] { Delimiter }, StringSplitOptions.RemoveEmptyEntries); string[] values = SelectedValue.Split(new string[] { Delimiter }, StringSplitOptions.RemoveEmptyEntries);

120
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Primitives/Selector.cs

@ -48,85 +48,21 @@ namespace Microsoft.Windows.Controls.Primitives
set { SetValue(DelimiterProperty, value); } set { SetValue(DelimiterProperty, value); }
} }
#region SelectedItem public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(Selector), new UIPropertyMetadata(null));
public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(Selector), new UIPropertyMetadata(null, OnSelectedItemChanged));
public object SelectedItem public object SelectedItem
{ {
get { return (object)GetValue(SelectedItemProperty); } get { return (object)GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); } set { SetValue(SelectedItemProperty, value); }
} }
private static void OnSelectedItemChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) //Make Read Only
{ public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems", typeof(IList), typeof(Selector), new UIPropertyMetadata(null));
Selector selector = o as Selector;
if (selector != null)
selector.OnSelectedItemChanged((object)e.OldValue, (object)e.NewValue);
}
protected virtual void OnSelectedItemChanged(object oldValue, object newValue)
{
// TODO: Add your property changed side-effects. Descendants can override as well.
}
#endregion //SelectedItem
#region SelectedItems
public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems", typeof(IList), typeof(Selector), new UIPropertyMetadata(null, OnSelectedItemsChanged));
public IList SelectedItems public IList SelectedItems
{ {
get { return (IList)GetValue(SelectedItemsProperty); } get { return (IList)GetValue(SelectedItemsProperty); }
set { SetValue(SelectedItemsProperty, value); } set { SetValue(SelectedItemsProperty, value); }
} }
private static void OnSelectedItemsChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
Selector selector = o as Selector;
if (selector != null)
selector.OnSelectedItemsChanged((IList)e.OldValue, (IList)e.NewValue);
}
protected virtual void OnSelectedItemsChanged(IList oldValue, IList newValue)
{
if (oldValue != null)
{
var collection = oldValue as INotifyCollectionChanged;
if (collection != null)
collection.CollectionChanged -= SelectedItems_CollectionChanged;
}
if (newValue != null)
{
var collection = newValue as INotifyCollectionChanged;
if (collection != null)
collection.CollectionChanged += SelectedItems_CollectionChanged;
}
}
void SelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
var item = e.NewItems[0];
OnSelectedItemsCollectionChanged(item, false);
}
else if (e.Action == NotifyCollectionChangedAction.Remove)
{
var item = e.OldItems[0];
OnSelectedItemsCollectionChanged(item, true);
}
}
protected virtual void OnSelectedItemsCollectionChanged(object item, bool remove)
{
//var selectorItem = ItemContainerGenerator.ContainerFromItem(item) as SelectorItem;
//selectorItem.IsSelected = !remove;
UpdateSelectedValue(item, remove);
}
#endregion SelectedItems
#region SelectedValue #region SelectedValue
public static readonly DependencyProperty SelectedValueProperty = DependencyProperty.Register("SelectedValue", typeof(string), typeof(Selector), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSelectedValueChanged)); public static readonly DependencyProperty SelectedValueProperty = DependencyProperty.Register("SelectedValue", typeof(string), typeof(Selector), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSelectedValueChanged));
@ -155,22 +91,17 @@ namespace Microsoft.Windows.Controls.Primitives
#endregion //SelectedValue #endregion //SelectedValue
public static readonly DependencyProperty SelectedValuePathProperty = DependencyProperty.Register("SelectedValuePath", typeof(string), typeof(Selector), new UIPropertyMetadata(null)); public static readonly DependencyProperty ValueMemberPathProperty = DependencyProperty.Register("ValueMemberPath", typeof(string), typeof(Selector), new UIPropertyMetadata(null));
public string SelectedValuePath public string ValueMemberPath
{ {
get { return (string)GetValue(SelectedValuePathProperty); } get { return (string)GetValue(ValueMemberPathProperty); }
set { SetValue(SelectedValuePathProperty, value); } set { SetValue(ValueMemberPathProperty, value); }
} }
#endregion //Properties #endregion //Properties
#region Base Class Overrides #region Base Class Overrides
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
}
protected override bool IsItemItsOwnContainerOverride(object item) protected override bool IsItemItsOwnContainerOverride(object item)
{ {
return item is SelectorItem; return item is SelectorItem;
@ -189,9 +120,9 @@ namespace Microsoft.Windows.Controls.Primitives
var value = item; var value = item;
//let's check if we can find a value on the item using the SelectedValuePath property //let's check if we can find a value on the item using the SelectedValuePath property
if (!String.IsNullOrEmpty(SelectedValuePath)) if (!String.IsNullOrEmpty(ValueMemberPath))
{ {
var property = item.GetType().GetProperty(SelectedValuePath); var property = item.GetType().GetProperty(ValueMemberPath);
if (property != null) if (property != null)
{ {
value = property.GetValue(item, null); value = property.GetValue(item, null);
@ -253,13 +184,14 @@ namespace Microsoft.Windows.Controls.Primitives
#endregion //Event Handlers #endregion //Event Handlers
#region Methods #region Methods
protected object GetItemValue(object item) protected object GetItemValue(object item)
{ {
if (!String.IsNullOrEmpty(SelectedValuePath)) if (!String.IsNullOrEmpty(ValueMemberPath))
{ {
var property = item.GetType().GetProperty(SelectedValuePath); var property = item.GetType().GetProperty(ValueMemberPath);
if (property != null) if (property != null)
return property.GetValue(item, null); return property.GetValue(item, null);
} }
@ -309,9 +241,6 @@ namespace Microsoft.Windows.Controls.Primitives
private void UpdateSelectedItem(object item) private void UpdateSelectedItem(object item)
{ {
if (_surpressSelectionChanged)
return;
SelectedItem = item; SelectedItem = item;
} }
@ -331,8 +260,9 @@ namespace Microsoft.Windows.Controls.Primitives
private void UpdateSelectedValue(object item, bool remove) private void UpdateSelectedValue(object item, bool remove)
{ {
if (SelectedValue == null) //make sure we have a selected value, or we will get an exception
SelectedValue = String.Empty; if (String.IsNullOrEmpty(SelectedValue))
UpdateSelectedValue(String.Empty);
var value = GetItemValue(item); var value = GetItemValue(item);
var resolvedValue = GetDelimitedValue(value); var resolvedValue = GetDelimitedValue(value);
@ -349,7 +279,9 @@ namespace Microsoft.Windows.Controls.Primitives
updateValue = SelectedValue + resolvedValue; updateValue = SelectedValue + resolvedValue;
} }
UpdateSelectedValue(updateValue); //if the SelectedValue is the same as the updated value then just ignore it
if (!SelectedValue.Equals(value))
UpdateSelectedValue(updateValue);
} }
private void UpdateSelectedValue(string value) private void UpdateSelectedValue(string value)
@ -360,8 +292,7 @@ namespace Microsoft.Windows.Controls.Primitives
_surpressSelectedValueChanged = true; _surpressSelectedValueChanged = true;
if (!SelectedValue.Equals(value)) SelectedValue = value;
SelectedValue = value;
_surpressSelectedValueChanged = false; _surpressSelectedValueChanged = false;
} }
@ -370,12 +301,11 @@ namespace Microsoft.Windows.Controls.Primitives
{ {
_surpressSelectionChanged = true; _surpressSelectionChanged = true;
//first we have to unselect everything
UnselectAll();
if (!String.IsNullOrEmpty(SelectedValue)) if (!String.IsNullOrEmpty(SelectedValue))
{ {
//first we have to unselect everything
UnselectAll();
string[] values = SelectedValue.Split(new string[] { Delimiter }, StringSplitOptions.RemoveEmptyEntries); string[] values = SelectedValue.Split(new string[] { Delimiter }, StringSplitOptions.RemoveEmptyEntries);
foreach (string value in values) foreach (string value in values)
{ {
@ -389,7 +319,7 @@ namespace Microsoft.Windows.Controls.Primitives
} }
} }
_surpressSelectionChanged = true; _surpressSelectionChanged = false;
} }
private void UnselectAll() private void UnselectAll()
@ -411,13 +341,13 @@ namespace Microsoft.Windows.Controls.Primitives
protected object ResolveItemByValue(string value) protected object ResolveItemByValue(string value)
{ {
if (!String.IsNullOrEmpty(SelectedValuePath)) if (!String.IsNullOrEmpty(ValueMemberPath))
{ {
if (ItemsSource != null) if (ItemsSource != null)
{ {
foreach (object item in ItemsSource) foreach (object item in ItemsSource)
{ {
var property = item.GetType().GetProperty(SelectedValuePath); var property = item.GetType().GetProperty(ValueMemberPath);
if (property != null) if (property != null)
{ {
var propertyValue = property.GetValue(item, null); var propertyValue = property.GetValue(item, null);

Loading…
Cancel
Save