Browse Source

BREAKING CHANGES: CheckComboBox and CheckListBox now derives from same Selector base class.

pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
d073b12f3c
  1. 153
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBox.cs
  2. 17
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxCheckedChangedEventArgs.cs
  3. 81
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Implementation/CheckListBoxItem.cs
  4. 32
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CheckListBox/Themes/Generic.xaml
  5. 62
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Primitives/Selector.cs
  6. 2
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj

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

@ -1,18 +1,10 @@
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.ComponentModel;
using System.Collections;
using System.Windows;
using Microsoft.Windows.Controls.Primitives;
namespace Microsoft.Windows.Controls
{
public class CheckListBox : ItemsControl
public class CheckListBox : Selector
{
private bool _surpressSelectionChanged;
#region Constructors
static CheckListBox()
@ -22,146 +14,9 @@ namespace Microsoft.Windows.Controls
public CheckListBox()
{
CheckedItems = new List<object>();
AddHandler(CheckListBox.CheckedEvent, new RoutedEventHandler(CheckListBox_Checked));
AddHandler(CheckListBox.UncheckedEvent, new RoutedEventHandler(CheckListBox_Unchecked));
}
#endregion //Constructors
#region Properties
public static readonly DependencyProperty CheckedMemberPathProperty = DependencyProperty.Register("CheckedMemberPath", typeof(string), typeof(CheckListBox), new UIPropertyMetadata(null));
public string CheckedMemberPath
{
get { return (string)GetValue(CheckedMemberPathProperty); }
set { SetValue(CheckedMemberPathProperty, value); }
}
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(CheckListBox), new PropertyMetadata((ICommand)null));
[TypeConverter(typeof(CommandConverter))]
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
#region CheckedItem
public static readonly DependencyProperty CheckedItemProperty = DependencyProperty.Register("CheckedItem", typeof(object), typeof(CheckListBox), new UIPropertyMetadata(null, OnCheckedItemChanged));
public object CheckedItem
{
get { return (object)GetValue(CheckedItemProperty); }
set { SetValue(CheckedItemProperty, value); }
}
private static void OnCheckedItemChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
CheckListBox checkListBox = o as CheckListBox;
if (checkListBox != null)
checkListBox.OnCheckedItemChanged((object)e.OldValue, (object)e.NewValue);
}
protected virtual void OnCheckedItemChanged(object oldValue, object newValue)
{
}
#endregion //CheckedItem
public IList CheckedItems { get; private set; }
#endregion //Properties
#region Base Class Overrides
protected override DependencyObject GetContainerForItemOverride()
{
return new CheckListBoxItem();
}
protected override bool IsItemItsOwnContainerOverride(object item)
{
return item is CheckListBoxItem;
}
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
_surpressSelectionChanged = true;
var checkListBoxItem = element as FrameworkElement;
if (!String.IsNullOrEmpty(CheckedMemberPath))
{
Binding isCheckedBinding = new Binding(CheckedMemberPath);
isCheckedBinding.Mode = BindingMode.TwoWay;
isCheckedBinding.Source = item;
checkListBoxItem.SetBinding(CheckListBoxItem.IsCheckedProperty, isCheckedBinding);
}
base.PrepareContainerForItemOverride(element, item);
_surpressSelectionChanged = false;
}
#endregion //Base Class Overrides
#region Events
public static readonly RoutedEvent CheckedEvent = EventManager.RegisterRoutedEvent("CheckedEvent", RoutingStrategy.Bubble, typeof(SelectionChangedEventHandler), typeof(CheckListBox));
public static readonly RoutedEvent UncheckedEvent = EventManager.RegisterRoutedEvent("UncheckedEvent", RoutingStrategy.Bubble, typeof(SelectionChangedEventHandler), typeof(CheckListBox));
public static readonly RoutedEvent CheckedChangedEvent = EventManager.RegisterRoutedEvent("CheckedChanged", RoutingStrategy.Bubble, typeof(CheckListBoxCheckedChangedEventHandler), typeof(CheckListBox));
public event CheckListBoxCheckedChangedEventHandler CheckedChanged
{
add { AddHandler(CheckedChangedEvent, value); }
remove { RemoveHandler(CheckedChangedEvent, value); }
}
#endregion //Events
#region Methods
private void CheckListBox_Checked(object sender, RoutedEventArgs e)
{
var item = GetDataContextItem(e.OriginalSource);
SetCheckedItem(item);
CheckedItems.Add(item);
OnCheckedChanged();
}
private void CheckListBox_Unchecked(object sender, RoutedEventArgs e)
{
var item = GetDataContextItem(e.OriginalSource);
SetCheckedItem(item);
CheckedItems.Remove(item);
OnCheckedChanged();
}
private object GetDataContextItem(object source)
{
var selectedCheckListBoxItem = source as FrameworkElement;
if (selectedCheckListBoxItem != null)
return selectedCheckListBoxItem.DataContext;
else
return null;
}
private void SetCheckedItem(object source)
{
if (_surpressSelectionChanged)
return;
CheckedItem = source;
}
private void OnCheckedChanged()
{
if (_surpressSelectionChanged)
return;
RaiseEvent(new CheckListBoxCheckedChangedEventArgs(CheckListBox.CheckedChangedEvent, this, CheckedItem));
if (Command != null)
Command.Execute(CheckedItem);
}
#endregion //Methods
#endregion //Constructors
}
}

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

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

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

@ -1,81 +0,0 @@
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;
namespace Microsoft.Windows.Controls
{
public class CheckListBoxItem : ContentControl
{
static CheckListBoxItem()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CheckListBoxItem), new FrameworkPropertyMetadata(typeof(CheckListBoxItem)));
}
public CheckListBoxItem()
{
AddHandler(Mouse.MouseDownEvent, new MouseButtonEventHandler(CheckListBoxItem_MouseDown));
}
#region Properties
public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool), typeof(CheckListBoxItem), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsSelectedChanged));
public bool IsChecked
{
get { return (bool)GetValue(IsCheckedProperty); }
set { SetValue(IsCheckedProperty, value); }
}
private static void OnIsSelectedChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
CheckListBoxItem checkListBoxItem = o as CheckListBoxItem;
if (checkListBoxItem != null)
checkListBoxItem.OnIsSelectedChanged((bool)e.OldValue, (bool)e.NewValue);
}
protected virtual void OnIsSelectedChanged(bool oldValue, bool newValue)
{
if (newValue)
RaiseSelectionChangedEvent(new RoutedEventArgs(CheckListBox.CheckedEvent, this));
else
RaiseSelectionChangedEvent(new RoutedEventArgs(CheckListBox.UncheckedEvent, this));
}
#endregion //Properties
#region Events
public static readonly RoutedEvent SelectedEvent = CheckListBox.CheckedEvent.AddOwner(typeof(CheckListBoxItem));
public static readonly RoutedEvent UnselectedEvent = CheckListBox.UncheckedEvent.AddOwner(typeof(CheckListBoxItem));
#endregion
#region Event Hanlders
void CheckListBoxItem_MouseDown(object sender, MouseButtonEventArgs e)
{
IsChecked = !IsChecked;
}
#endregion //Event Hanlders
#region Methods
private void RaiseSelectionChangedEvent(RoutedEventArgs e)
{
base.RaiseEvent(e);
}
#endregion //Methods
}
}

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

@ -23,36 +23,4 @@
</Setter>
</Style>
<Style TargetType="{x:Type local:CheckListBoxItem}" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CheckListBoxItem}">
<Border x:Name="_background"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<CheckBox Name="PART_CheckBox" IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}}"
VerticalAlignment="Center" Focusable="False" Margin="3,1,5,1"/>
<Border Grid.Column="1">
<ContentPresenter Margin="2" ContentSource="Content" />
</Border>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="_background" Property="Background" Value="Blue"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>

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

@ -1,16 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Data;
using System.Collections;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.ComponentModel;
using System.Windows.Input;
namespace Microsoft.Windows.Controls.Primitives
{
public class Selector : ItemsControl
public class Selector : ItemsControl //should probably make this control an ICommandSource
{
#region Members
@ -25,13 +24,21 @@ namespace Microsoft.Windows.Controls.Primitives
{
SelectedItems = new ObservableCollection<object>();
AddHandler(Selector.SelectedEvent, new RoutedEventHandler(Selector_ItemSelected));
AddHandler(Selector.UnSelectedEvent, new RoutedEventHandler(Selector_ItemUnSelected));
AddHandler(Selector.UnSelectedEvent, new RoutedEventHandler(Selector_ItemUnselected));
}
#endregion //Constructors
#region Properties
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(CheckListBox), new PropertyMetadata((ICommand)null));
[TypeConverter(typeof(CommandConverter))]
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
public static readonly DependencyProperty DelimiterProperty = DependencyProperty.Register("Delimiter", typeof(string), typeof(Selector), new UIPropertyMetadata(","));
public string Delimiter
{
@ -189,14 +196,14 @@ namespace Microsoft.Windows.Controls.Primitives
#region Events
public static readonly RoutedEvent SelectedEvent = EventManager.RegisterRoutedEvent("SelectedEvent", RoutingStrategy.Bubble, typeof(SelectionChangedEventHandler), typeof(Selector));
public static readonly RoutedEvent UnSelectedEvent = EventManager.RegisterRoutedEvent("UnSelectedEvent", RoutingStrategy.Bubble, typeof(SelectionChangedEventHandler), typeof(Selector));
public static readonly RoutedEvent SelectedEvent = EventManager.RegisterRoutedEvent("SelectedEvent", RoutingStrategy.Bubble, typeof(SelectedItemChangedEventHandler), typeof(Selector));
public static readonly RoutedEvent UnSelectedEvent = EventManager.RegisterRoutedEvent("UnSelectedEvent", RoutingStrategy.Bubble, typeof(SelectedItemChangedEventHandler), typeof(Selector));
public static readonly RoutedEvent SelectionChangedEvent = EventManager.RegisterRoutedEvent("SelectionChanged", RoutingStrategy.Bubble, typeof(SelectionChangedEventHandler), typeof(Selector));
public event SelectionChangedEventHandler SelectionChanged
public static readonly RoutedEvent SelectedItemChangedEvent = EventManager.RegisterRoutedEvent("SelectedItemChanged", RoutingStrategy.Bubble, typeof(SelectedItemChangedEventHandler), typeof(Selector));
public event SelectedItemChangedEventHandler SelectionItemChanged
{
add { AddHandler(SelectionChangedEvent, value); }
remove { RemoveHandler(SelectionChangedEvent, value); }
add { AddHandler(SelectedItemChangedEvent, value); }
remove { RemoveHandler(SelectedItemChangedEvent, value); }
}
#endregion //Events
@ -208,7 +215,7 @@ namespace Microsoft.Windows.Controls.Primitives
OnItemSelected(e.OriginalSource, false);
}
protected virtual void Selector_ItemUnSelected(object sender, RoutedEventArgs e)
protected virtual void Selector_ItemUnselected(object sender, RoutedEventArgs e)
{
OnItemSelected(e.OriginalSource, true);
}
@ -248,19 +255,18 @@ namespace Microsoft.Windows.Controls.Primitives
{
var item = GetDataContextItem(source);
Update(item, remove);
if (remove)
OnSelectionChanged(new List<object>() { item }, new List<object>());
else
OnSelectionChanged(new List<object>(), new List<object>() { item });
RaiseSelectionItemChangedEvent(item);
}
private void OnSelectionChanged(IList removedItems, IList addedItems)
protected virtual void RaiseSelectionItemChangedEvent(object item)
{
if (_surpressSelectionChanged)
return;
RaiseEvent(new SelectionChangedEventArgs(Selector.SelectionChangedEvent, removedItems, addedItems));
RaiseEvent(new SelectedItemChangedEventArgs(Selector.SelectedItemChangedEvent, this, item));
if (Command != null)
Command.Execute(SelectedItem);
}
protected virtual void Update(object item, bool remove)
@ -371,4 +377,16 @@ namespace Microsoft.Windows.Controls.Primitives
#endregion //Methods
}
public delegate void SelectedItemChangedEventHandler(object sender, SelectedItemChangedEventArgs e);
public class SelectedItemChangedEventArgs : RoutedEventArgs
{
public object Item { get; private set; }
public SelectedItemChangedEventArgs(RoutedEvent routedEvent, object source, object item)
: base(routedEvent, source)
{
Item = item;
}
}
}

2
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj

@ -182,9 +182,7 @@
<Compile Include="CalculatorUpDown\Implementation\CalculatorUpDown.cs" />
<Compile Include="Calculator\Implementation\Calculator.cs" />
<Compile Include="Calculator\Implementation\CalculatorCommands.cs" />
<Compile Include="CheckListBox\Implementation\CheckListBoxItem.cs" />
<Compile Include="CheckListBox\Implementation\CheckListBox.cs" />
<Compile Include="CheckListBox\Implementation\CheckListBoxCheckedChangedEventArgs.cs" />
<Compile Include="ChildWindow\Implementation\ChildWindow.cs" />
<Compile Include="ChildWindow\Implementation\WindowStartupLocation.cs" />
<Compile Include="ChildWindow\Implementation\WindowState.cs" />

Loading…
Cancel
Save