Browse Source

made some slight changes to the CheckListBox/CheckComboBox.

IMPORTANT: There is a SelectedItems property for both controls, but it is intended to be ReadOnly.  You cannot data bind to ReadOnly DependencyProperties, so in order to enable the scenario of data binding to the SelectedItems from a ViewModel, I had to leave it as a public get/set property. For this binding to work correctly, you MUST set the binding Mode=OneWayToSource for the SelectedItems property.
pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
7c10f04894
  1. 55
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckComboBox/Implementation/CheckComboBox.cs
  2. 127
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Primitives/Selector.cs

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

@ -1,4 +1,5 @@
using System; using System;
using System.Linq;
using System.Windows; using System.Windows;
using Microsoft.Windows.Controls.Primitives; using Microsoft.Windows.Controls.Primitives;
@ -56,66 +57,22 @@ namespace Microsoft.Windows.Controls
#region Base Class Overrides #region Base Class Overrides
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
}
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(); UpdateText();
} }
#endregion //Base Class Overrides #endregion //Base Class Overrides
#region Methods #region Methods
private void UpdateDisplayText(object item, bool remove) private void UpdateText()
{ {
if (Text == null) string newValue = String.Join(Delimiter, SelectedItems.Cast<object>().Select(x => GetItemDisplayValue(x)));
Text = String.Empty;
var displayText = GetItemDisplayValue(item);
var resolvedDisplayText = GetDelimitedValue(displayText);
string updatedText = Text;
if (remove)
{
if (Text.Contains(resolvedDisplayText))
updatedText = Text.Replace(resolvedDisplayText, "");
}
else
{
if (!Text.Contains(resolvedDisplayText))
updatedText = Text + resolvedDisplayText;
}
UpdateText(updatedText); if (String.IsNullOrEmpty(Text) || !Text.Equals(newValue))
} Text = newValue;
private void UpdateText(string text)
{
if (String.IsNullOrEmpty(Text))
Text = string.Empty;
if (!Text.Equals(text))
Text = text;
}
private void UpdateTextFromSelectedValue()
{
UpdateText(String.Empty);
if (!String.IsNullOrEmpty(SelectedValue))
{
string[] values = SelectedValue.Split(new string[] { Delimiter }, StringSplitOptions.RemoveEmptyEntries);
foreach (string value in values)
{
var item = ResolveItemByValue(value);
UpdateDisplayText(item, false);
}
}
} }
protected object GetItemDisplayValue(object item) protected object GetItemDisplayValue(object item)

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

@ -2,6 +2,7 @@
using System.Collections; using System.Collections;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.Linq;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Data; using System.Windows.Data;
@ -24,10 +25,19 @@ namespace Microsoft.Windows.Controls.Primitives
public Selector() public Selector()
{ {
SelectedItems = new ObservableCollection<object>(); SelectedItems = new ObservableCollection<object>();
//ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged);
AddHandler(Selector.SelectedEvent, new RoutedEventHandler(Selector_ItemSelected)); AddHandler(Selector.SelectedEvent, new RoutedEventHandler(Selector_ItemSelected));
AddHandler(Selector.UnSelectedEvent, new RoutedEventHandler(Selector_ItemUnselected)); AddHandler(Selector.UnSelectedEvent, new RoutedEventHandler(Selector_ItemUnselected));
} }
//void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
//{
// if (ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
// {
// UpdateSelectedItemsFromSelectedValue();
// }
//}
#endregion //Constructors #endregion //Constructors
#region Properties #region Properties
@ -54,7 +64,8 @@ namespace Microsoft.Windows.Controls.Primitives
set { SetValue(SelectedItemProperty, value); } set { SetValue(SelectedItemProperty, value); }
} }
//Make Read Only //Since you cannot data bind to ReadOnly DependencyProperty, I am leaving this a public get/set DP. This will allow you to data bind to the SelectedItems from a ViewModel, but it is
//intended to be ReadOnly. So you MUST set the binding Mode=OneWayToSource. Otherwise it will not behave as expected.
public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems", typeof(IList), typeof(Selector), new UIPropertyMetadata(null)); public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems", typeof(IList), typeof(Selector), new UIPropertyMetadata(null));
public IList SelectedItems public IList SelectedItems
{ {
@ -90,9 +101,7 @@ namespace Microsoft.Windows.Controls.Primitives
if (_surpressSelectedValueChanged) if (_surpressSelectedValueChanged)
return; return;
//if we are controling the selections from the SelectedValue we can only update the IsSelected property of the items if the containers have been generated UpdateSelectedItemsFromSelectedValue();
if (ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
UpdateSelectedItemsFromSelectedValue();
} }
#endregion //SelectedValue #endregion //SelectedValue
@ -121,53 +130,31 @@ namespace Microsoft.Windows.Controls.Primitives
protected override void PrepareContainerForItemOverride(DependencyObject element, object item) protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{ {
_surpressSelectionChanged = true; _surpressSelectionChanged = true;
bool isSelected = false;
var selectorItem = element as FrameworkElement; var selectorItem = element as FrameworkElement;
//first try resolving SelectorItem.IsSelected by data binding to the SelectedMemeberPath property //first try resolving SelectorItem.IsSelected by data binding to the SelectedMemeberPath property
if (!String.IsNullOrEmpty(SelectedMemberPath)) if (!String.IsNullOrEmpty(SelectedMemberPath))
{ {
Binding selectedBinding = new Binding(SelectedMemberPath) Binding selectedBinding = new Binding(SelectedMemberPath)
{ {
Mode = BindingMode.TwoWay, Mode = BindingMode.TwoWay,
Source = item Source = item
}; };
selectorItem.SetBinding(SelectorItem.IsSelectedProperty, selectedBinding); selectorItem.SetBinding(SelectorItem.IsSelectedProperty, selectedBinding);
} }
else
{
var value = item;
//let's check if we can find a value on the item using the SelectedValuePath property
if (!String.IsNullOrEmpty(ValueMemberPath))
{
var property = item.GetType().GetProperty(ValueMemberPath);
if (property != null)
{
value = property.GetValue(item, null);
}
}
//now check to see if the SelectedValue string contains our value. If it does then set Selector.IsSelected to true //now let's search the SelectedItems for the current item. If it's there then mark it as selected
if (!String.IsNullOrEmpty(SelectedValue) && SelectedValue.Contains(GetDelimitedValue(value))) if (SelectedItems != null)
{ {
isSelected = true; foreach (object selectedItem in SelectedItems)
}
else if (SelectedItems != null)
{ {
//if we get here we could find the value in the SelectedValue property, so lets search the SelectedItems for a match //a match was found so select it and get the hell out of here
foreach (object selectedItem in SelectedItems) if (item.Equals(selectedItem))
{ {
//a match was found so select it and get the hell out of here selectorItem.SetValue(SelectorItem.IsSelectedProperty, true);
if (value.Equals(GetItemValue(selectedItem))) break;
{
isSelected = true;
break;
}
} }
} }
selectorItem.SetValue(SelectorItem.IsSelectedProperty, isSelected);
} }
base.PrepareContainerForItemOverride(element, item); base.PrepareContainerForItemOverride(element, item);
@ -255,7 +242,7 @@ namespace Microsoft.Windows.Controls.Primitives
{ {
UpdateSelectedItem(item); UpdateSelectedItem(item);
UpdateSelectedItems(item, remove); UpdateSelectedItems(item, remove);
UpdateSelectedValue(item, remove); UpdateSelectedValue();
} }
private void UpdateSelectedItem(object item) private void UpdateSelectedItem(object item)
@ -277,33 +264,7 @@ namespace Microsoft.Windows.Controls.Primitives
} }
} }
private void UpdateSelectedValue(object item, bool remove) private void UpdateSelectedValue()
{
//make sure we have a selected value, or we will get an exception
if (SelectedValue == null)
UpdateSelectedValue(String.Empty);
var value = GetItemValue(item);
var resolvedValue = GetDelimitedValue(value);
string updateValue = SelectedValue;
if (remove)
{
if (SelectedValue.Contains(resolvedValue))
updateValue = SelectedValue.Replace(resolvedValue, "");
}
else
{
if (!SelectedValue.Contains(resolvedValue))
updateValue = SelectedValue + resolvedValue;
}
//if the SelectedValue is the same as the updated value then just ignore it
if (!SelectedValue.Equals(updateValue))
UpdateSelectedValue(updateValue);
}
private void UpdateSelectedValue(string value)
{ {
//get out of here if we don't want to set the SelectedValue //get out of here if we don't want to set the SelectedValue
if (_ignoreSetSelectedValue) if (_ignoreSetSelectedValue)
@ -311,7 +272,10 @@ namespace Microsoft.Windows.Controls.Primitives
_surpressSelectedValueChanged = true; _surpressSelectedValueChanged = true;
SelectedValue = value; string newValue = String.Join(Delimiter, SelectedItems.Cast<object>().Select(x => GetItemValue(x)));
if (String.IsNullOrEmpty(SelectedValue) || !SelectedValue.Equals(newValue))
SelectedValue = newValue;
_surpressSelectedValueChanged = false; _surpressSelectedValueChanged = false;
} }
@ -321,7 +285,7 @@ namespace Microsoft.Windows.Controls.Primitives
_surpressSelectionChanged = true; _surpressSelectionChanged = true;
//first we have to unselect everything //first we have to unselect everything
UnselectAll(); ClearSelectedItems();
if (!String.IsNullOrEmpty(SelectedValue)) if (!String.IsNullOrEmpty(SelectedValue))
{ {
@ -329,11 +293,18 @@ namespace Microsoft.Windows.Controls.Primitives
foreach (string value in values) foreach (string value in values)
{ {
var item = ResolveItemByValue(value); var item = ResolveItemByValue(value);
var selectorItem = ItemContainerGenerator.ContainerFromItem(item) as SelectorItem;
if (selectorItem != null) if (item != null)
{ {
if (!selectorItem.IsSelected) SelectedItems.Add(item);
selectorItem.IsSelected = true;
//now try to select it in the list
var selectorItem = ItemContainerGenerator.ContainerFromItem(item) as SelectorItem;
if (selectorItem != null)
{
if (!selectorItem.IsSelected)
selectorItem.IsSelected = true;
}
} }
} }
} }
@ -341,7 +312,17 @@ namespace Microsoft.Windows.Controls.Primitives
_surpressSelectionChanged = false; _surpressSelectionChanged = false;
} }
private void UnselectAll() private void ClearSelectedItems()
{
if (SelectedItems != null)
SelectedItems.Clear();
else
SelectedItems = new ObservableCollection<object>();
UnselectAllInternal();
}
private void UnselectAllInternal()
{ {
_ignoreSetSelectedValue = true; _ignoreSetSelectedValue = true;

Loading…
Cancel
Save