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> <StackPanel>
<extToolkit:CheckComboBox x:Name="_combo" HorizontalAlignment="Center" VerticalAlignment="Center" <extToolkit:CheckComboBox x:Name="_combo" HorizontalAlignment="Center" VerticalAlignment="Center"
DisplayMemberPath="Color" DisplayMemberPath="Color"
SelectedValuePath="Level" ValueMemberPath="Level"
SelectedItems="{Binding SelectedItems}" /> SelectedValue="{Binding SelectedValue}"
SelectedItems="{Binding SelectedItems}"/>
<!--<extToolkit:CheckComboBox x:Name="_combo" HorizontalAlignment="Center" VerticalAlignment="Center" <!--<extToolkit:CheckComboBox x:Name="_combo" HorizontalAlignment="Center" VerticalAlignment="Center"
SelectedValue="{Binding SelectedValues}" />--> SelectedValue="{Binding SelectedValues}" />-->
@ -39,12 +40,20 @@
<extToolkit:CheckListBox x:Name="_listBox" Height="250" <extToolkit:CheckListBox x:Name="_listBox" Height="250"
DisplayMemberPath="Color" DisplayMemberPath="Color"
SelectedValuePath="Level" ValueMemberPath="Level"
SelectedItems="{Binding SelectedItems}" /> SelectedMemberPath="IsSelected"
SelectedValue="{Binding SelectedValue}"
SelectedItems="{Binding SelectedItems}"/>
<!--<extToolkit:CheckListBox x:Name="_listBox" Height="250" <!--<extToolkit:CheckListBox x:Name="_listBox" Height="250"
SelectedValue="{Binding SelectedValues}" SelectedValue="{Binding SelectedValues}"
/>--> />-->
<TextBlock Text="{Binding SelectedValue}" />
<ListBox ItemsSource="{Binding SelectedItems}" Height="50" />
<Button Click="Button_Click">Set</Button>
</StackPanel> </StackPanel>
</Grid> </Grid>
</infControls:DemoView> </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>(); List<Person> colors = new List<Person>();
colors.Add(new Person(System.Windows.Media.Colors.Red, 0)); 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.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.Green, 4));
colors.Add(new Person(System.Windows.Media.Colors.Red, 5)); colors.Add(new Person(System.Windows.Media.Colors.Red, 5));
colors.Add(new Person(System.Windows.Media.Colors.Purple, 6)); 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,"; //_combo.SelectedValue = "1,3,5,7,9,";
_listBox.ItemsSource = colors; _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,"; private string _selectedValues;// = "1,3,5,7,9,";
public string SelectedValues public string SelectedValue
{ {
get get
{ {
@ -76,23 +81,31 @@ namespace Samples.Modules.Color.Views
set set
{ {
_selectedValues = value; _selectedValues = value;
OnPropertyChanged("SelectedValue");
} }
} }
private ObservableCollection<Person> _selectedItems = new ObservableCollection<Person>() private ObservableCollection<Person> _selectedItems = new ObservableCollection<Person>();
{
new Person(System.Windows.Media.Colors.Red, 0),
new Person(System.Windows.Media.Colors.Coral, 2)
};
public ObservableCollection<Person> SelectedItems public ObservableCollection<Person> SelectedItems
{ {
get { return _selectedItems; } get { return _selectedItems; }
set set
{ {
_selectedItems = value; _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 public class Person : INotifyPropertyChanged

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

@ -1,12 +1,11 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Data; using System.Windows.Data;
using System.ComponentModel;
using System.Windows.Input; using System.Windows.Input;
using System.Collections.Specialized;
namespace Microsoft.Windows.Controls.Primitives namespace Microsoft.Windows.Controls.Primitives
{ {
@ -63,6 +62,13 @@ namespace Microsoft.Windows.Controls.Primitives
set { SetValue(SelectedItemsProperty, value); } 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 #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));
@ -117,38 +123,52 @@ namespace Microsoft.Windows.Controls.Primitives
_surpressSelectionChanged = true; _surpressSelectionChanged = true;
bool isSelected = false; bool isSelected = false;
var selectorItem = element as FrameworkElement; var selectorItem = element as FrameworkElement;
var value = item;
//let's check if we can find a value on the item using the SelectedValuePath property //first try resolving SelectorItem.IsSelected by data binding to the SelectedMemeberPath property
if (!String.IsNullOrEmpty(ValueMemberPath)) if (!String.IsNullOrEmpty(SelectedMemberPath))
{ {
var property = item.GetType().GetProperty(ValueMemberPath); Binding selectedBinding = new Binding(SelectedMemberPath)
if (property != null) {
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 //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))) if (!String.IsNullOrEmpty(SelectedValue) && SelectedValue.Contains(GetDelimitedValue(value)))
{ {
isSelected = true; isSelected = true;
} }
else if (SelectedItems != null) 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)
{ {
//a match was found so select it and get the hell out of here //if we get here we could find the value in the SelectedValue property, so lets search the SelectedItems for a match
if (value.Equals(GetItemValue(selectedItem))) foreach (object selectedItem in SelectedItems)
{ {
isSelected = true; //a match was found so select it and get the hell out of here
break; if (value.Equals(GetItemValue(selectedItem)))
{
isSelected = true;
break;
}
} }
} }
}
selectorItem.SetValue(SelectorItem.IsSelectedProperty, isSelected); selectorItem.SetValue(SelectorItem.IsSelectedProperty, isSelected);
}
base.PrepareContainerForItemOverride(element, item); base.PrepareContainerForItemOverride(element, item);
_surpressSelectionChanged = false; _surpressSelectionChanged = false;
@ -184,7 +204,6 @@ 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)
@ -261,7 +280,7 @@ namespace Microsoft.Windows.Controls.Primitives
private void UpdateSelectedValue(object item, bool remove) private void UpdateSelectedValue(object item, bool remove)
{ {
//make sure we have a selected value, or we will get an exception //make sure we have a selected value, or we will get an exception
if (String.IsNullOrEmpty(SelectedValue)) if (SelectedValue == null)
UpdateSelectedValue(String.Empty); UpdateSelectedValue(String.Empty);
var value = GetItemValue(item); 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 the SelectedValue is the same as the updated value then just ignore it
if (!SelectedValue.Equals(value)) if (!SelectedValue.Equals(updateValue))
UpdateSelectedValue(updateValue); UpdateSelectedValue(updateValue);
} }

Loading…
Cancel
Save