|
|
|
@ -6,6 +6,7 @@ using System.Windows.Controls; |
|
|
|
using System.Windows.Data; |
|
|
|
using System.ComponentModel; |
|
|
|
using System.Windows.Input; |
|
|
|
using System.Collections.Specialized; |
|
|
|
|
|
|
|
namespace Microsoft.Windows.Controls.Primitives |
|
|
|
{ |
|
|
|
@ -88,18 +89,44 @@ namespace Microsoft.Windows.Controls.Primitives |
|
|
|
|
|
|
|
protected virtual void OnSelectedItemsChanged(IList oldValue, IList newValue) |
|
|
|
{ |
|
|
|
|
|
|
|
} |
|
|
|
if (oldValue != null) |
|
|
|
{ |
|
|
|
var collection = oldValue as INotifyCollectionChanged; |
|
|
|
if (collection != null) |
|
|
|
collection.CollectionChanged -= SelectedItems_CollectionChanged; |
|
|
|
} |
|
|
|
|
|
|
|
#endregion SelectedItems
|
|
|
|
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); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static readonly DependencyProperty SelectedMemberPathProperty = DependencyProperty.Register("SelectedMemberPath", typeof(string), typeof(Selector), new UIPropertyMetadata(null)); |
|
|
|
public string SelectedMemberPath |
|
|
|
protected virtual void OnSelectedItemsCollectionChanged(object item, bool remove) |
|
|
|
{ |
|
|
|
get { return (string)GetValue(SelectedMemberPathProperty); } |
|
|
|
set { SetValue(SelectedMemberPathProperty, value); } |
|
|
|
//var selectorItem = ItemContainerGenerator.ContainerFromItem(item) as SelectorItem;
|
|
|
|
//selectorItem.IsSelected = !remove;
|
|
|
|
UpdateSelectedValue(item, remove); |
|
|
|
} |
|
|
|
|
|
|
|
#endregion SelectedItems
|
|
|
|
|
|
|
|
#region SelectedValue
|
|
|
|
|
|
|
|
public static readonly DependencyProperty SelectedValueProperty = DependencyProperty.Register("SelectedValue", typeof(string), typeof(Selector), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSelectedValueChanged)); |
|
|
|
@ -157,36 +184,41 @@ namespace Microsoft.Windows.Controls.Primitives |
|
|
|
protected override void PrepareContainerForItemOverride(DependencyObject element, object item) |
|
|
|
{ |
|
|
|
_surpressSelectionChanged = true; |
|
|
|
bool isSelected = false; |
|
|
|
var selectorItem = element as FrameworkElement; |
|
|
|
var value = item; |
|
|
|
|
|
|
|
//let's check if we can find a value on the item using the SelectedValuePath property
|
|
|
|
if (!String.IsNullOrEmpty(SelectedValuePath)) |
|
|
|
{ |
|
|
|
var property = item.GetType().GetProperty(SelectedValuePath); |
|
|
|
if (property != null) |
|
|
|
{ |
|
|
|
value = property.GetValue(item, null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//first try resolving SelectorItem.IsSelected by data binding to the SelectedMemeberPath property
|
|
|
|
if (!String.IsNullOrEmpty(SelectedMemberPath)) |
|
|
|
//now check to see if the SelectedValue string contains our value. If it does then set Selector.IsSelected to true
|
|
|
|
if (!String.IsNullOrEmpty(SelectedValue) && SelectedValue.Contains(GetDelimitedValue(value))) |
|
|
|
{ |
|
|
|
Binding selectedBinding = new Binding(SelectedMemberPath); |
|
|
|
selectedBinding.Mode = BindingMode.TwoWay; |
|
|
|
selectedBinding.Source = item; |
|
|
|
selectorItem.SetBinding(SelectorItem.IsSelectedProperty, selectedBinding); |
|
|
|
isSelected = true; |
|
|
|
} |
|
|
|
else |
|
|
|
else if (SelectedItems != null) |
|
|
|
{ |
|
|
|
//if the SelectedMemberPath property is not set, then default to the value of the item
|
|
|
|
var value = item; |
|
|
|
|
|
|
|
//now let's check if we can find a value on the item using the SelectedValuePath property
|
|
|
|
if (!String.IsNullOrEmpty(SelectedValuePath)) |
|
|
|
//if we get here we could find the value in the SelectedValue property, so lets search the SelectedItems for a match
|
|
|
|
foreach (object selectedItem in SelectedItems) |
|
|
|
{ |
|
|
|
var property = item.GetType().GetProperty(SelectedValuePath); |
|
|
|
if (property != null) |
|
|
|
//a match was found so select it and get the hell out of here
|
|
|
|
if (value.Equals(GetItemValue(selectedItem))) |
|
|
|
{ |
|
|
|
value = property.GetValue(item, null); |
|
|
|
isSelected = true; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//now check to see if the SelectedValue string contains our value. If it does then set Selector.IsSelected to true
|
|
|
|
if (!String.IsNullOrEmpty(SelectedValue) && SelectedValue.Contains(GetDelimitedValue(value))) |
|
|
|
selectorItem.SetValue(SelectorItem.IsSelectedProperty, true); |
|
|
|
} |
|
|
|
|
|
|
|
selectorItem.SetValue(SelectorItem.IsSelectedProperty, isSelected); |
|
|
|
|
|
|
|
base.PrepareContainerForItemOverride(element, item); |
|
|
|
_surpressSelectionChanged = false; |
|
|
|
} |
|
|
|
@ -405,7 +437,7 @@ namespace Microsoft.Windows.Controls.Primitives |
|
|
|
public delegate void SelectedItemChangedEventHandler(object sender, SelectedItemChangedEventArgs e); |
|
|
|
public class SelectedItemChangedEventArgs : RoutedEventArgs |
|
|
|
{ |
|
|
|
public bool IsSelected {get;private set;} |
|
|
|
public bool IsSelected { get; private set; } |
|
|
|
public object Item { get; private set; } |
|
|
|
|
|
|
|
public SelectedItemChangedEventArgs(RoutedEvent routedEvent, object source, object item, bool isSelected) |
|
|
|
|