Browse Source

working on the CheckListBox. You bind your collection to the ItemsSource property. Set the DisplayMemberPath, and the CheckedMemberPath. It also supports Commanding. Set the Command property to your command. The parameter will default to the SelectedItem, which is the item that was just checked/unchecked. It also has a SelectionChganged event. You can obtain the object the was check/unchecked from the e.Item property of the event args.

pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
096fd815ce
  1. 93
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBox.cs
  2. 33
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxItem.cs
  3. 17
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxSelectionChangedEventArgs.cs
  4. 5
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Themes/Generic.xaml
  5. 1
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj

93
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBox.cs

@ -1,26 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.ComponentModel;
using System.Collections;
namespace Microsoft.Windows.Controls
{
public class CheckListBox : MultiSelector
public class CheckListBox : ItemsControl
{
private bool _surpressSelectionChanged;
#region Constructors
static CheckListBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CheckListBox), new FrameworkPropertyMetadata(typeof(CheckListBox)));
@ -28,9 +22,13 @@ namespace Microsoft.Windows.Controls
public CheckListBox()
{
SelectedItems = new List<object>();
AddHandler(CheckListBox.SelectedEvent, new RoutedEventHandler(CheckListBox_Selected));
AddHandler(CheckListBox.UnselectedEvent, new RoutedEventHandler(CheckListBox_Unselected));
}
#endregion //Constructors
#region Properties
public static readonly DependencyProperty CheckedMemberPathProperty = DependencyProperty.Register("CheckedMemberPath", typeof(string), typeof(CheckListBox), new UIPropertyMetadata(null));
@ -48,6 +46,31 @@ namespace Microsoft.Windows.Controls
set { SetValue(CommandProperty, value); }
}
#region SelectedItem
public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(CheckListBox), new UIPropertyMetadata(null, OnSelectedItemChanged));
public object SelectedItem
{
get { return (object)GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
private static void OnSelectedItemChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
CheckListBox checkListBox = o as CheckListBox;
if (checkListBox != null)
checkListBox.OnSelectedItemChanged((object)e.OldValue, (object)e.NewValue);
}
protected virtual void OnSelectedItemChanged(object oldValue, object newValue)
{
OnSelectionChanged();
}
#endregion //SelectedItem
public IList SelectedItems { get; private set; }
#endregion //Properties
#region Base Class Overrides
@ -65,7 +88,6 @@ namespace Microsoft.Windows.Controls
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
_surpressSelectionChanged = true;
var checkListBoxItem = element as FrameworkElement;
if (!String.IsNullOrEmpty(CheckedMemberPath))
{
@ -74,16 +96,55 @@ namespace Microsoft.Windows.Controls
checkListBoxItem.SetBinding(CheckListBoxItem.IsSelectedProperty, isCheckedBinding);
}
base.PrepareContainerForItemOverride(element, item);
_surpressSelectionChanged = false;
}
protected override void OnSelectionChanged(SelectionChangedEventArgs e)
#endregion //Base Class Overrides
#region Events
public static readonly RoutedEvent SelectedEvent = EventManager.RegisterRoutedEvent("Selected", RoutingStrategy.Bubble, typeof(SelectionChangedEventHandler), typeof(CheckListBox));
public static readonly RoutedEvent UnselectedEvent = EventManager.RegisterRoutedEvent("Unselected", RoutingStrategy.Bubble, typeof(SelectionChangedEventHandler), typeof(CheckListBox));
public static readonly RoutedEvent SelectionChangedEvent = EventManager.RegisterRoutedEvent("SelectionChanged", RoutingStrategy.Bubble, typeof(CheckListBoxSelectionChangedEventHandler), typeof(CheckListBox));
public event CheckListBoxSelectionChangedEventHandler SelectionChanged
{
add { AddHandler(SelectionChangedEvent, value); }
remove { RemoveHandler(SelectionChangedEvent, value); }
}
#endregion //Events
void CheckListBox_Selected(object sender, RoutedEventArgs e)
{
if (!_surpressSelectionChanged)
base.OnSelectionChanged(e);
SetSelectedItem(e.OriginalSource);
SelectedItems.Add(SelectedItem);
}
#endregion //Base Class Overrides
void CheckListBox_Unselected(object sender, RoutedEventArgs e)
{
SetSelectedItem(e.OriginalSource);
SelectedItems.Remove(SelectedItem);
}
private void SetSelectedItem(object source)
{
if (_surpressSelectionChanged)
return;
var selectedCheckListBoxItem = source as FrameworkElement;
if (selectedCheckListBoxItem != null)
SelectedItem = selectedCheckListBoxItem.DataContext;
}
private void OnSelectionChanged()
{
if (_surpressSelectionChanged)
return;
RaiseEvent(new CheckListBoxSelectionChangedEventArgs(CheckListBox.SelectionChangedEvent, this, SelectedItem));
if (Command != null)
Command.Execute(SelectedItem);
}
}
}

33
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxItem.cs

@ -24,7 +24,7 @@ namespace Microsoft.Windows.Controls
#region Properties
public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(CheckListBoxItem), new UIPropertyMetadata(false, OnIsSelectedChanged));
public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(CheckListBoxItem), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsSelectedChanged));
public bool IsSelected
{
get { return (bool)GetValue(IsSelectedProperty); }
@ -41,44 +41,23 @@ namespace Microsoft.Windows.Controls
protected virtual void OnIsSelectedChanged(bool oldValue, bool newValue)
{
if (newValue)
OnSelected(new RoutedEventArgs(Selector.SelectedEvent, this));
RaiseSelectionChangedEvent(new RoutedEventArgs(CheckListBox.SelectedEvent, this));
else
OnUnselected(new RoutedEventArgs(Selector.UnselectedEvent, this));
RaiseSelectionChangedEvent(new RoutedEventArgs(CheckListBox.UnselectedEvent, this));
}
#endregion //Properties
#region Events
public static readonly RoutedEvent SelectedEvent = Selector.SelectedEvent.AddOwner(typeof(CheckListBoxItem));
public event RoutedEventHandler Selected
{
add { base.AddHandler(SelectedEvent, value); }
remove { base.RemoveHandler(SelectedEvent, value); }
}
public static readonly RoutedEvent UnselectedEvent = Selector.UnselectedEvent.AddOwner(typeof(CheckListBoxItem));
public event RoutedEventHandler Unselected
{
add { base.AddHandler(UnselectedEvent, value); }
remove { base.RemoveHandler(UnselectedEvent, value); }
}
public static readonly RoutedEvent SelectedEvent = CheckListBox.SelectedEvent.AddOwner(typeof(CheckListBoxItem));
public static readonly RoutedEvent UnselectedEvent = CheckListBox.UnselectedEvent.AddOwner(typeof(CheckListBoxItem));
#endregion
#region Methods
protected virtual void OnSelected(RoutedEventArgs e)
{
this.OnIsSelectedChanged(true, e);
}
protected virtual void OnUnselected(RoutedEventArgs e)
{
this.OnIsSelectedChanged(false, e);
}
private void OnIsSelectedChanged(bool newValue, RoutedEventArgs e)
private void RaiseSelectionChangedEvent(RoutedEventArgs e)
{
base.RaiseEvent(e);
}

17
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxSelectionChangedEventArgs.cs

@ -0,0 +1,17 @@
using System;
using System.Windows;
namespace Microsoft.Windows.Controls
{
public delegate void CheckListBoxSelectionChangedEventHandler(object sender, CheckListBoxSelectionChangedEventArgs e);
public class CheckListBoxSelectionChangedEventArgs : RoutedEventArgs
{
public object Item { get; private set; }
public CheckListBoxSelectionChangedEventArgs(RoutedEvent routedEvent, object source, object item)
: base(routedEvent, source)
{
Item = item;
}
}
}

5
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Themes/Generic.xaml

@ -34,9 +34,8 @@
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<CheckBox Name="PART_CheckBox" IsChecked="{Binding IsSelected, RelativeSource={RelativeSource TemplatedParent}}"
Command="{Binding Command, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CheckListBox}}"
CommandParameter="{Binding}" VerticalAlignment="Center" Focusable="False" Margin="1,1,5,1"/>
<CheckBox Name="PART_CheckBox" IsChecked="{Binding IsSelected, RelativeSource={RelativeSource TemplatedParent}}"
VerticalAlignment="Center" Focusable="False" Margin="1,1,5,1"/>
<Border Grid.Column="1">
<ContentPresenter Margin="2" ContentSource="Content" />
</Border>

1
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj

@ -152,6 +152,7 @@
<Compile Include="Calculator\Implementation\CalculatorCommands.cs" />
<Compile Include="CheckListBox\Implementation\CheckListBoxItem.cs" />
<Compile Include="CheckListBox\Implementation\CheckListBox.cs" />
<Compile Include="CheckListBox\Implementation\CheckListBoxSelectionChangedEventArgs.cs" />
<Compile Include="ChildWindow\Implementation\ChildWindow.cs" />
<Compile Include="ChildWindow\Implementation\WindowStartupLocation.cs" />
<Compile Include="ChildWindow\Implementation\WindowState.cs" />

Loading…
Cancel
Save