Browse Source

SplitButton: added click event and Commanding support.

pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
d9f171528f
  1. 162
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/SplitButton/SplitButton.cs
  2. 2
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Themes/Generic.xaml

162
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/SplitButton/SplitButton.cs

@ -3,13 +3,14 @@ using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Controls.Primitives; using System.Windows.Controls.Primitives;
using System.ComponentModel;
namespace Microsoft.Windows.Controls namespace Microsoft.Windows.Controls
{ {
public class SplitButton : ContentControl public class SplitButton : ContentControl, ICommandSource
{ {
#region Members #region Members
Button _actionButton;
ToggleButton _toggleButton; ToggleButton _toggleButton;
Popup _popup; Popup _popup;
@ -51,7 +52,7 @@ namespace Microsoft.Windows.Controls
protected virtual void OnDropDownContentChanged(object oldValue, object newValue) protected virtual void OnDropDownContentChanged(object oldValue, object newValue)
{ {
// TODO: Add your property changed side-effects. Descendants can override as well. // TODO: Add your property changed side-effects. Descendants can override as well.
} }
#endregion //DropDownContent #endregion //DropDownContent
@ -80,31 +81,44 @@ namespace Microsoft.Windows.Controls
#endregion //Properties #endregion //Properties
#region Events
public static readonly RoutedEvent ClickEvent = EventManager.RegisterRoutedEvent("Click", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SplitButton));
public event RoutedEventHandler Click
{
add { AddHandler(ClickEvent, value); }
remove { RemoveHandler(ClickEvent, value); }
}
#endregion //Events
#region Base Class Overrides #region Base Class Overrides
public override void OnApplyTemplate() public override void OnApplyTemplate()
{ {
base.OnApplyTemplate(); base.OnApplyTemplate();
_actionButton = (Button)GetTemplateChild("PART_ActionButton");
_actionButton.Click += ActionButton_Click;
_toggleButton = (ToggleButton)GetTemplateChild("PART_ToggleButton"); _toggleButton = (ToggleButton)GetTemplateChild("PART_ToggleButton");
_toggleButton.Click += ToggleButton_Click;
_popup = (Popup)GetTemplateChild("PART_Popup"); _popup = (Popup)GetTemplateChild("PART_Popup");
_popup.Opened += Popup_Opened; //_popup.Opened += Popup_Opened;
} }
#endregion //Base Class Overrides #endregion //Base Class Overrides
#region Event Handlers #region Event Handlers
void ToggleButton_Click(object sender, RoutedEventArgs e) void ActionButton_Click(object sender, RoutedEventArgs e)
{ {
OnClick();
} }
void Popup_Opened(object sender, EventArgs e) void Popup_Opened(object sender, EventArgs e)
{ {
} }
private void OnKeyDown(object sender, KeyEventArgs e) private void OnKeyDown(object sender, KeyEventArgs e)
@ -112,7 +126,6 @@ namespace Microsoft.Windows.Controls
switch (e.Key) switch (e.Key)
{ {
case Key.Escape: case Key.Escape:
case Key.Tab:
{ {
CloseDropDown(); CloseDropDown();
break; break;
@ -125,10 +138,88 @@ namespace Microsoft.Windows.Controls
CloseDropDown(); CloseDropDown();
} }
void CanExecuteChanged(object sender, EventArgs e)
{
if (Command != null)
{
RoutedCommand command = Command as RoutedCommand;
// If a RoutedCommand.
if (command != null)
IsEnabled = command.CanExecute(CommandParameter, CommandTarget) ? true : false;
// If a not RoutedCommand.
else
IsEnabled = Command.CanExecute(CommandParameter) ? true : false;
}
}
#endregion //Event Handlers #endregion //Event Handlers
#region Methods #region Methods
#region Protected
protected virtual void OnClick()
{
RaiseClickEvent();
RaiseCommand();
}
#endregion //Protected
#region Private
/// <summary>
/// Raises the click event.
/// </summary>
private void RaiseClickEvent()
{
RoutedEventArgs args = new RoutedEventArgs(SplitButton.ClickEvent, this);
RaiseEvent(args);
}
/// <summary>
/// Raises the command's Execute event.
/// </summary>
private void RaiseCommand()
{
if (Command != null)
{
RoutedCommand routedCommand = Command as RoutedCommand;
if (routedCommand == null)
((ICommand)Command).Execute(CommandParameter);
else
routedCommand.Execute(CommandParameter, CommandTarget);
}
}
/// <summary>
/// Unhooks a command from the Command property.
/// </summary>
/// <param name="oldCommand">The old command.</param>
/// <param name="newCommand">The new command.</param>
private void UnhookCommand(ICommand oldCommand, ICommand newCommand)
{
EventHandler handler = CanExecuteChanged;
oldCommand.CanExecuteChanged -= handler;
}
/// <summary>
/// Hooks up a command to the CanExecuteChnaged event handler.
/// </summary>
/// <param name="oldCommand">The old command.</param>
/// <param name="newCommand">The new command.</param>
private void HookUpCommand(ICommand oldCommand, ICommand newCommand)
{
EventHandler handler = new EventHandler(CanExecuteChanged);
canExecuteChangedHandler = handler;
if (newCommand != null)
newCommand.CanExecuteChanged += canExecuteChangedHandler;
}
/// <summary>
/// Closes the drop down.
/// </summary>
private void CloseDropDown() private void CloseDropDown()
{ {
if (IsOpen) if (IsOpen)
@ -136,6 +227,57 @@ namespace Microsoft.Windows.Controls
ReleaseMouseCapture(); ReleaseMouseCapture();
} }
#endregion //Private
#endregion //Methods #endregion //Methods
#region ICommandSource Members
// Keeps a copy of the CanExecuteChnaged handler so it doesn't get garbage collected.
private static EventHandler canExecuteChangedHandler;
#region Command
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(SplitButton), new PropertyMetadata((ICommand)null, OnCommandChanged));
[TypeConverter(typeof(CommandConverter))]
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
SplitButton splitButton = d as SplitButton;
if (splitButton != null)
splitButton.OnCommandChanged((ICommand)e.OldValue, (ICommand)e.NewValue);
}
protected virtual void OnCommandChanged(ICommand oldValue, ICommand newValue)
{
// If old command is not null, then we need to remove the handlers.
if (oldValue != null)
UnhookCommand(oldValue, newValue);
HookUpCommand(oldValue, newValue);
}
#endregion //Command
public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(SplitButton), new PropertyMetadata(null));
public object CommandParameter
{
get { return GetValue(CommandParameterProperty); }
set { SetValue(CommandParameterProperty, value); }
}
public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(SplitButton), new PropertyMetadata(null));
public IInputElement CommandTarget
{
get { return (IInputElement)GetValue(CommandTargetProperty); }
set { SetValue(CommandTargetProperty, value); }
}
#endregion
} }
} }

2
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Themes/Generic.xaml

@ -1216,7 +1216,7 @@
<ColumnDefinition Width="*" /> <ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<Button x:Name="PART_HeaderButton" Style="{StaticResource SplitButtonStyle}" > <Button x:Name="PART_ActionButton" Style="{StaticResource SplitButtonStyle}" >
<ContentPresenter Name="Content" Margin="{TemplateBinding Padding}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="true" /> <ContentPresenter Name="Content" Margin="{TemplateBinding Padding}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="true" />
</Button> </Button>
<ToggleButton x:Name="PART_ToggleButton" Grid.Column="1" IsTabStop="False" <ToggleButton x:Name="PART_ToggleButton" Grid.Column="1" IsTabStop="False"

Loading…
Cancel
Save