Browse Source

Added SelectedMemberPath to CheckListBox and CheckComboBox

pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
201a9243f8
  1. 19
      ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.Color/Views/HomeView.xaml
  2. 37
      ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.Color/Views/HomeView.xaml.cs
  3. 73
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Primitives/Selector.cs

19
ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.Color/Views/HomeView.xaml

@ -25,9 +25,10 @@
<StackPanel>
<extToolkit:CheckComboBox x:Name="_combo" HorizontalAlignment="Center" VerticalAlignment="Center"
DisplayMemberPath="Color"
SelectedValuePath="Level"
SelectedItems="{Binding SelectedItems}" />
DisplayMemberPath="Color"
ValueMemberPath="Level"
SelectedValue="{Binding SelectedValue}"
SelectedItems="{Binding SelectedItems}"/>
<!--<extToolkit:CheckComboBox x:Name="_combo" HorizontalAlignment="Center" VerticalAlignment="Center"
SelectedValue="{Binding SelectedValues}" />-->
@ -39,12 +40,20 @@
<extToolkit:CheckListBox x:Name="_listBox" Height="250"
DisplayMemberPath="Color"
SelectedValuePath="Level"
SelectedItems="{Binding SelectedItems}" />
ValueMemberPath="Level"
SelectedMemberPath="IsSelected"
SelectedValue="{Binding SelectedValue}"
SelectedItems="{Binding SelectedItems}"/>
<!--<extToolkit:CheckListBox x:Name="_listBox" Height="250"
SelectedValue="{Binding SelectedValues}"
/>-->
<TextBlock Text="{Binding SelectedValue}" />
<ListBox ItemsSource="{Binding SelectedItems}" Height="50" />
<Button Click="Button_Click">Set</Button>
</StackPanel>
</Grid>
</infControls:DemoView>

37
ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.Color/Views/HomeView.xaml.cs

@ -34,9 +34,9 @@ namespace Samples.Modules.Color.Views
List<Person> colors = new List<Person>();
colors.Add(new Person(System.Windows.Media.Colors.Red, 0));
colors.Add(new Person(System.Windows.Media.Colors.Purple, 1));
colors.Add(new Person(System.Windows.Media.Colors.Purple, 1) { IsSelected = true });
colors.Add(new Person(System.Windows.Media.Colors.Coral, 2));
colors.Add(new Person(System.Windows.Media.Colors.MidnightBlue, 3));
colors.Add(new Person(System.Windows.Media.Colors.MidnightBlue, 3) { IsSelected = true });
colors.Add(new Person(System.Windows.Media.Colors.Green, 4));
colors.Add(new Person(System.Windows.Media.Colors.Red, 5));
colors.Add(new Person(System.Windows.Media.Colors.Purple, 6));
@ -60,14 +60,19 @@ namespace Samples.Modules.Color.Views
//_combo.SelectedValue = "1,3,5,7,9,";
_listBox.ItemsSource = colors;
//_listBox.DelimitedValue = "1,3,5,7,9,";
//_listBox.SelectedValue = "1,3,5,7,9,";
}
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
(DataContext as Data).SelectedValue = "1,3,5,7,9,";
}
}
public class Data
public class Data: INotifyPropertyChanged
{
private string _selectedValues;// = "1,3,5,7,9,";
public string SelectedValues
public string SelectedValue
{
get
{
@ -76,23 +81,31 @@ namespace Samples.Modules.Color.Views
set
{
_selectedValues = value;
OnPropertyChanged("SelectedValue");
}
}
private ObservableCollection<Person> _selectedItems = new ObservableCollection<Person>()
{
new Person(System.Windows.Media.Colors.Red, 0),
new Person(System.Windows.Media.Colors.Coral, 2)
};
private ObservableCollection<Person> _selectedItems = new ObservableCollection<Person>();
public ObservableCollection<Person> SelectedItems
{
get { return _selectedItems; }
set
{
_selectedItems = value;
OnPropertyChanged("SelectedItems");
}
}
}
public Data()
{
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public class Person : INotifyPropertyChanged

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

@ -1,12 +1,11 @@
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.ComponentModel;
using System.Windows.Input;
using System.Collections.Specialized;
namespace Microsoft.Windows.Controls.Primitives
{
@ -63,6 +62,13 @@ namespace Microsoft.Windows.Controls.Primitives
set { SetValue(SelectedItemsProperty, value); }
}
public static readonly DependencyProperty SelectedMemberPathProperty = DependencyProperty.Register("SelectedMemberPath", typeof(string), typeof(Selector), new UIPropertyMetadata(null));
public string SelectedMemberPath
{
get { return (string)GetValue(SelectedMemberPathProperty); }
set { SetValue(SelectedMemberPathProperty, value); }
}
#region SelectedValue
public static readonly DependencyProperty SelectedValueProperty = DependencyProperty.Register("SelectedValue", typeof(string), typeof(Selector), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSelectedValueChanged));
@ -117,38 +123,52 @@ namespace Microsoft.Windows.Controls.Primitives
_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(ValueMemberPath))
//first try resolving SelectorItem.IsSelected by data binding to the SelectedMemeberPath property
if (!String.IsNullOrEmpty(SelectedMemberPath))
{
var property = item.GetType().GetProperty(ValueMemberPath);
if (property != null)
Binding selectedBinding = new Binding(SelectedMemberPath)
{
Mode = BindingMode.TwoWay,
Source = item
};
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))
{
value = property.GetValue(item, null);
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
if (!String.IsNullOrEmpty(SelectedValue) && SelectedValue.Contains(GetDelimitedValue(value)))
{
isSelected = true;
}
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
foreach (object selectedItem in SelectedItems)
//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)))
{
isSelected = true;
}
else if (SelectedItems != null)
{
//a match was found so select it and get the hell out of here
if (value.Equals(GetItemValue(selectedItem)))
//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)
{
isSelected = true;
break;
//a match was found so select it and get the hell out of here
if (value.Equals(GetItemValue(selectedItem)))
{
isSelected = true;
break;
}
}
}
}
selectorItem.SetValue(SelectorItem.IsSelectedProperty, isSelected);
selectorItem.SetValue(SelectorItem.IsSelectedProperty, isSelected);
}
base.PrepareContainerForItemOverride(element, item);
_surpressSelectionChanged = false;
@ -184,7 +204,6 @@ namespace Microsoft.Windows.Controls.Primitives
#endregion //Event Handlers
#region Methods
protected object GetItemValue(object item)
@ -261,7 +280,7 @@ namespace Microsoft.Windows.Controls.Primitives
private void UpdateSelectedValue(object item, bool remove)
{
//make sure we have a selected value, or we will get an exception
if (String.IsNullOrEmpty(SelectedValue))
if (SelectedValue == null)
UpdateSelectedValue(String.Empty);
var value = GetItemValue(item);
@ -280,7 +299,7 @@ namespace Microsoft.Windows.Controls.Primitives
}
//if the SelectedValue is the same as the updated value then just ignore it
if (!SelectedValue.Equals(value))
if (!SelectedValue.Equals(updateValue))
UpdateSelectedValue(updateValue);
}

Loading…
Cancel
Save