Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Framework / System / Windows / Controls / Primitives / ToggleButton.cs / 1458001 / ToggleButton.cs
//----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//---------------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Threading;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Controls.Primitives;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using MS.Internal.KnownBoxes;
// Disable CS3001: Warning as Error: not CLS-compliant
#pragma warning disable 3001
namespace System.Windows.Controls.Primitives
{
///
/// ToggleButton
///
[DefaultEvent("Checked")]
public class ToggleButton : ButtonBase
{
#region Constructors
static ToggleButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ToggleButton), new FrameworkPropertyMetadata(typeof(ToggleButton)));
_dType = DependencyObjectType.FromSystemTypeInternal(typeof(ToggleButton));
}
///
/// Default ToggleButton constructor
///
///
/// Automatic determination of current Dispatcher. Use alternative constructor
/// that accepts a Dispatcher for best performance.
///
public ToggleButton() : base()
{
}
#endregion
#region Properties and Events
///
/// Checked event
///
public static readonly RoutedEvent CheckedEvent = EventManager.RegisterRoutedEvent("Checked", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ToggleButton));
///
/// Unchecked event
///
public static readonly RoutedEvent UncheckedEvent = EventManager.RegisterRoutedEvent("Unchecked", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ToggleButton));
///
/// Indeterminate event
///
public static readonly RoutedEvent IndeterminateEvent = EventManager.RegisterRoutedEvent("Indeterminate", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ToggleButton));
///
/// Add / Remove Checked handler
///
[Category("Behavior")]
public event RoutedEventHandler Checked
{
add
{
AddHandler(CheckedEvent, value);
}
remove
{
RemoveHandler(CheckedEvent, value);
}
}
///
/// Add / Remove Unchecked handler
///
[Category("Behavior")]
public event RoutedEventHandler Unchecked
{
add
{
AddHandler(UncheckedEvent, value);
}
remove
{
RemoveHandler(UncheckedEvent, value);
}
}
///
/// Add / Remove Indeterminate handler
///
[Category("Behavior")]
public event RoutedEventHandler Indeterminate
{
add
{
AddHandler(IndeterminateEvent, value);
}
remove
{
RemoveHandler(IndeterminateEvent, value);
}
}
///
/// The DependencyProperty for the IsChecked property.
/// Flags: BindsTwoWayByDefault
/// Default Value: false
///
public static readonly DependencyProperty IsCheckedProperty =
DependencyProperty.Register(
"IsChecked",
typeof(bool?),
typeof(ToggleButton),
new FrameworkPropertyMetadata(
BooleanBoxes.FalseBox,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal,
new PropertyChangedCallback(OnIsCheckedChanged)));
///
/// Indicates whether the ToggleButton is checked
///
[Category("Appearance")]
[TypeConverter(typeof(NullableBoolConverter))]
[Localizability(LocalizationCategory.None, Readability = Readability.Unreadable)]
public bool? IsChecked
{
get
{
// Because Nullable unboxing is very slow (uses reflection) first we cast to bool
object value = GetValue(IsCheckedProperty);
if (value == null)
return new Nullable();
else
return new Nullable((bool)value);
}
set
{
SetValue(IsCheckedProperty, value.HasValue ? BooleanBoxes.Box(value.Value) : null);
}
}
private static object OnGetIsChecked(DependencyObject d) {return ((ToggleButton)d).IsChecked;}
///
/// Called when IsChecked is changed on "d."
///
/// The object on which the property was changed.
/// EventArgs that contains the old and new values for this property
private static void OnIsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ToggleButton button = (ToggleButton)d;
bool? oldValue = (bool?) e.OldValue;
bool? newValue = (bool?) e.NewValue;
//doing soft casting here because the peer can be that of RadioButton and it is not derived from
//ToggleButtonAutomationPeer - specifically to avoid implementing TogglePattern
ToggleButtonAutomationPeer peer = UIElementAutomationPeer.FromElement(button) as ToggleButtonAutomationPeer;
if (peer != null)
{
peer.RaiseToggleStatePropertyChangedEvent(oldValue, newValue);
}
if (newValue == true)
{
button.OnChecked(new RoutedEventArgs(CheckedEvent));
}
else if (newValue == false)
{
button.OnUnchecked(new RoutedEventArgs(UncheckedEvent));
}
else
{
button.OnIndeterminate(new RoutedEventArgs(IndeterminateEvent));
}
button.UpdateVisualState();
}
///
/// Called when IsChecked becomes true.
///
/// Event arguments for the routed event that is raised by the default implementation of this method.
protected virtual void OnChecked(RoutedEventArgs e)
{
RaiseEvent(e);
}
///
/// Called when IsChecked becomes false.
///
/// Event arguments for the routed event that is raised by the default implementation of this method.
protected virtual void OnUnchecked(RoutedEventArgs e)
{
RaiseEvent(e);
}
///
/// Called when IsChecked becomes null.
///
/// Event arguments for the routed event that is raised by the default implementation of this method.
protected virtual void OnIndeterminate(RoutedEventArgs e)
{
RaiseEvent(e);
}
///
/// The DependencyProperty for the IsThreeState property.
/// Flags: None
/// Default Value: false
///
public static readonly DependencyProperty IsThreeStateProperty =
DependencyProperty.Register(
"IsThreeState",
typeof(bool),
typeof(ToggleButton),
new FrameworkPropertyMetadata(BooleanBoxes.FalseBox));
///
/// The IsThreeState property determines whether the control supports two or three states.
/// IsChecked property can be set to null as a third state when IsThreeState is true
///
[Bindable(true), Category("Behavior")]
public bool IsThreeState
{
get { return (bool) GetValue(IsThreeStateProperty); }
set { SetValue(IsThreeStateProperty, BooleanBoxes.Box(value)); }
}
#endregion
#region Override methods
///
/// Creates AutomationPeer ( )
///
protected override AutomationPeer OnCreateAutomationPeer()
{
return new ToggleButtonAutomationPeer(this);
}
///
/// This override method is called when the control is clicked by mouse or keyboard
///
protected override void OnClick()
{
OnToggle();
base.OnClick();
}
#endregion
#region Method Overrides
internal override void ChangeVisualState(bool useTransitions)
{
base.ChangeVisualState(useTransitions);
// Update the Check state group
var isChecked = IsChecked;
if (isChecked == true)
{
VisualStateManager.GoToState(this, VisualStates.StateChecked, useTransitions);
}
else if (isChecked == false)
{
VisualStateManager.GoToState(this, VisualStates.StateUnchecked, useTransitions);
}
else
{
// isChecked is null
VisualStates.GoToState(this, useTransitions, VisualStates.StateIndeterminate, VisualStates.StateUnchecked);
}
}
///
/// Gives a string representation of this object.
///
public override string ToString()
{
string typeText = this.GetType().ToString();
string contentText = String.Empty;
bool? isChecked = false;
bool valuesDefined = false;
// Accessing ToggleButton properties may be thread sensitive
if (CheckAccess())
{
contentText = GetPlainText();
isChecked = IsChecked;
valuesDefined = true;
}
else
{
//Not on dispatcher, try posting to the dispatcher with 20ms timeout
Dispatcher.Invoke(DispatcherPriority.Send, new TimeSpan(0, 0, 0, 0, 20), new DispatcherOperationCallback(delegate(object o)
{
contentText = GetPlainText();
isChecked = IsChecked;
valuesDefined = true;
return null;
}), null);
}
// If Content and isChecked are defined
if (valuesDefined)
{
return SR.Get(SRID.ToStringFormatString_ToggleButton, typeText, contentText, isChecked.HasValue ? isChecked.Value.ToString() : "null");
}
// Not able to access the dispatcher
return typeText;
}
#endregion
#region Protected virtual methods
///
/// This vitrual method is called from OnClick(). ToggleButton toggles IsChecked property.
/// Subclasses can override this method to implement their own toggle behavior
///
protected internal virtual void OnToggle()
{
// If IsChecked == true && IsThreeState == true ---> IsChecked = null
// If IsChecked == true && IsThreeState == false ---> IsChecked = false
// If IsChecked == false ---> IsChecked = true
// If IsChecked == null ---> IsChecked = false
bool? isChecked;
if (IsChecked == true)
isChecked = IsThreeState ? (bool?)null : (bool?)false;
else // false or null
isChecked = IsChecked.HasValue; // HasValue returns true if IsChecked==false
SetCurrentValueInternal(IsCheckedProperty, isChecked);
}
#endregion
#region Data
#endregion
#region Accessibility
#endregion
#region DTypeThemeStyleKey
// Returns the DependencyObjectType for the registered ThemeStyleKey's default
// value. Controls will override this method to return approriate types.
internal override DependencyObjectType DTypeThemeStyleKey
{
get { return _dType; }
}
private static DependencyObjectType _dType;
#endregion DTypeThemeStyleKey
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//---------------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Threading;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Controls.Primitives;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using MS.Internal.KnownBoxes;
// Disable CS3001: Warning as Error: not CLS-compliant
#pragma warning disable 3001
namespace System.Windows.Controls.Primitives
{
///
/// ToggleButton
///
[DefaultEvent("Checked")]
public class ToggleButton : ButtonBase
{
#region Constructors
static ToggleButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ToggleButton), new FrameworkPropertyMetadata(typeof(ToggleButton)));
_dType = DependencyObjectType.FromSystemTypeInternal(typeof(ToggleButton));
}
///
/// Default ToggleButton constructor
///
///
/// Automatic determination of current Dispatcher. Use alternative constructor
/// that accepts a Dispatcher for best performance.
///
public ToggleButton() : base()
{
}
#endregion
#region Properties and Events
///
/// Checked event
///
public static readonly RoutedEvent CheckedEvent = EventManager.RegisterRoutedEvent("Checked", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ToggleButton));
///
/// Unchecked event
///
public static readonly RoutedEvent UncheckedEvent = EventManager.RegisterRoutedEvent("Unchecked", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ToggleButton));
///
/// Indeterminate event
///
public static readonly RoutedEvent IndeterminateEvent = EventManager.RegisterRoutedEvent("Indeterminate", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ToggleButton));
///
/// Add / Remove Checked handler
///
[Category("Behavior")]
public event RoutedEventHandler Checked
{
add
{
AddHandler(CheckedEvent, value);
}
remove
{
RemoveHandler(CheckedEvent, value);
}
}
///
/// Add / Remove Unchecked handler
///
[Category("Behavior")]
public event RoutedEventHandler Unchecked
{
add
{
AddHandler(UncheckedEvent, value);
}
remove
{
RemoveHandler(UncheckedEvent, value);
}
}
///
/// Add / Remove Indeterminate handler
///
[Category("Behavior")]
public event RoutedEventHandler Indeterminate
{
add
{
AddHandler(IndeterminateEvent, value);
}
remove
{
RemoveHandler(IndeterminateEvent, value);
}
}
///
/// The DependencyProperty for the IsChecked property.
/// Flags: BindsTwoWayByDefault
/// Default Value: false
///
public static readonly DependencyProperty IsCheckedProperty =
DependencyProperty.Register(
"IsChecked",
typeof(bool?),
typeof(ToggleButton),
new FrameworkPropertyMetadata(
BooleanBoxes.FalseBox,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal,
new PropertyChangedCallback(OnIsCheckedChanged)));
///
/// Indicates whether the ToggleButton is checked
///
[Category("Appearance")]
[TypeConverter(typeof(NullableBoolConverter))]
[Localizability(LocalizationCategory.None, Readability = Readability.Unreadable)]
public bool? IsChecked
{
get
{
// Because Nullable unboxing is very slow (uses reflection) first we cast to bool
object value = GetValue(IsCheckedProperty);
if (value == null)
return new Nullable();
else
return new Nullable((bool)value);
}
set
{
SetValue(IsCheckedProperty, value.HasValue ? BooleanBoxes.Box(value.Value) : null);
}
}
private static object OnGetIsChecked(DependencyObject d) {return ((ToggleButton)d).IsChecked;}
///
/// Called when IsChecked is changed on "d."
///
/// The object on which the property was changed.
/// EventArgs that contains the old and new values for this property
private static void OnIsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ToggleButton button = (ToggleButton)d;
bool? oldValue = (bool?) e.OldValue;
bool? newValue = (bool?) e.NewValue;
//doing soft casting here because the peer can be that of RadioButton and it is not derived from
//ToggleButtonAutomationPeer - specifically to avoid implementing TogglePattern
ToggleButtonAutomationPeer peer = UIElementAutomationPeer.FromElement(button) as ToggleButtonAutomationPeer;
if (peer != null)
{
peer.RaiseToggleStatePropertyChangedEvent(oldValue, newValue);
}
if (newValue == true)
{
button.OnChecked(new RoutedEventArgs(CheckedEvent));
}
else if (newValue == false)
{
button.OnUnchecked(new RoutedEventArgs(UncheckedEvent));
}
else
{
button.OnIndeterminate(new RoutedEventArgs(IndeterminateEvent));
}
button.UpdateVisualState();
}
///
/// Called when IsChecked becomes true.
///
/// Event arguments for the routed event that is raised by the default implementation of this method.
protected virtual void OnChecked(RoutedEventArgs e)
{
RaiseEvent(e);
}
///
/// Called when IsChecked becomes false.
///
/// Event arguments for the routed event that is raised by the default implementation of this method.
protected virtual void OnUnchecked(RoutedEventArgs e)
{
RaiseEvent(e);
}
///
/// Called when IsChecked becomes null.
///
/// Event arguments for the routed event that is raised by the default implementation of this method.
protected virtual void OnIndeterminate(RoutedEventArgs e)
{
RaiseEvent(e);
}
///
/// The DependencyProperty for the IsThreeState property.
/// Flags: None
/// Default Value: false
///
public static readonly DependencyProperty IsThreeStateProperty =
DependencyProperty.Register(
"IsThreeState",
typeof(bool),
typeof(ToggleButton),
new FrameworkPropertyMetadata(BooleanBoxes.FalseBox));
///
/// The IsThreeState property determines whether the control supports two or three states.
/// IsChecked property can be set to null as a third state when IsThreeState is true
///
[Bindable(true), Category("Behavior")]
public bool IsThreeState
{
get { return (bool) GetValue(IsThreeStateProperty); }
set { SetValue(IsThreeStateProperty, BooleanBoxes.Box(value)); }
}
#endregion
#region Override methods
///
/// Creates AutomationPeer ( )
///
protected override AutomationPeer OnCreateAutomationPeer()
{
return new ToggleButtonAutomationPeer(this);
}
///
/// This override method is called when the control is clicked by mouse or keyboard
///
protected override void OnClick()
{
OnToggle();
base.OnClick();
}
#endregion
#region Method Overrides
internal override void ChangeVisualState(bool useTransitions)
{
base.ChangeVisualState(useTransitions);
// Update the Check state group
var isChecked = IsChecked;
if (isChecked == true)
{
VisualStateManager.GoToState(this, VisualStates.StateChecked, useTransitions);
}
else if (isChecked == false)
{
VisualStateManager.GoToState(this, VisualStates.StateUnchecked, useTransitions);
}
else
{
// isChecked is null
VisualStates.GoToState(this, useTransitions, VisualStates.StateIndeterminate, VisualStates.StateUnchecked);
}
}
///
/// Gives a string representation of this object.
///
public override string ToString()
{
string typeText = this.GetType().ToString();
string contentText = String.Empty;
bool? isChecked = false;
bool valuesDefined = false;
// Accessing ToggleButton properties may be thread sensitive
if (CheckAccess())
{
contentText = GetPlainText();
isChecked = IsChecked;
valuesDefined = true;
}
else
{
//Not on dispatcher, try posting to the dispatcher with 20ms timeout
Dispatcher.Invoke(DispatcherPriority.Send, new TimeSpan(0, 0, 0, 0, 20), new DispatcherOperationCallback(delegate(object o)
{
contentText = GetPlainText();
isChecked = IsChecked;
valuesDefined = true;
return null;
}), null);
}
// If Content and isChecked are defined
if (valuesDefined)
{
return SR.Get(SRID.ToStringFormatString_ToggleButton, typeText, contentText, isChecked.HasValue ? isChecked.Value.ToString() : "null");
}
// Not able to access the dispatcher
return typeText;
}
#endregion
#region Protected virtual methods
///
/// This vitrual method is called from OnClick(). ToggleButton toggles IsChecked property.
/// Subclasses can override this method to implement their own toggle behavior
///
protected internal virtual void OnToggle()
{
// If IsChecked == true && IsThreeState == true ---> IsChecked = null
// If IsChecked == true && IsThreeState == false ---> IsChecked = false
// If IsChecked == false ---> IsChecked = true
// If IsChecked == null ---> IsChecked = false
bool? isChecked;
if (IsChecked == true)
isChecked = IsThreeState ? (bool?)null : (bool?)false;
else // false or null
isChecked = IsChecked.HasValue; // HasValue returns true if IsChecked==false
SetCurrentValueInternal(IsCheckedProperty, isChecked);
}
#endregion
#region Data
#endregion
#region Accessibility
#endregion
#region DTypeThemeStyleKey
// Returns the DependencyObjectType for the registered ThemeStyleKey's default
// value. Controls will override this method to return approriate types.
internal override DependencyObjectType DTypeThemeStyleKey
{
get { return _dType; }
}
private static DependencyObjectType _dType;
#endregion DTypeThemeStyleKey
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ContextProperty.cs
- XmlLanguageConverter.cs
- WindowsImpersonationContext.cs
- WebPartCloseVerb.cs
- SymbolType.cs
- UserControlBuildProvider.cs
- WebBrowserProgressChangedEventHandler.cs
- WebRequestModuleElementCollection.cs
- CodeFieldReferenceExpression.cs
- EntityDataSource.cs
- GradientBrush.cs
- WorkflowApplicationUnloadedException.cs
- DataGridViewTextBoxColumn.cs
- StreamMarshaler.cs
- UnSafeCharBuffer.cs
- EventSourceCreationData.cs
- X509RawDataKeyIdentifierClause.cs
- HierarchicalDataSourceControl.cs
- TextEditorCharacters.cs
- User.cs
- XmlEncApr2001.cs
- SystemInfo.cs
- AttachedAnnotationChangedEventArgs.cs
- SmtpCommands.cs
- HttpRequestWrapper.cs
- WorkItem.cs
- Win32PrintDialog.cs
- Label.cs
- GridViewDeletedEventArgs.cs
- StateWorkerRequest.cs
- TransformerInfoCollection.cs
- DrawingContextWalker.cs
- WinFormsUtils.cs
- SHA256CryptoServiceProvider.cs
- X509CertificateCollection.cs
- CategoryEditor.cs
- UnauthorizedAccessException.cs
- ObjectTag.cs
- DebugViewWriter.cs
- SchemaCollectionPreprocessor.cs
- BindingSource.cs
- ServiceNotStartedException.cs
- AvTrace.cs
- DataGridViewTextBoxEditingControl.cs
- EmbeddedMailObject.cs
- IdentityVerifier.cs
- Animatable.cs
- RelationshipSet.cs
- Control.cs
- BehaviorService.cs
- SimpleApplicationHost.cs
- HtmlEncodedRawTextWriter.cs
- EntityClientCacheEntry.cs
- LinqDataSourceContextData.cs
- TiffBitmapEncoder.cs
- Context.cs
- HiddenFieldPageStatePersister.cs
- EventProviderWriter.cs
- XPathSelectionIterator.cs
- ObfuscateAssemblyAttribute.cs
- SrgsText.cs
- UriParserTemplates.cs
- LocatorGroup.cs
- SecurityHelper.cs
- TextSpanModifier.cs
- SqlCaseSimplifier.cs
- FullTextBreakpoint.cs
- Error.cs
- ExtensionSurface.cs
- AnnouncementEndpoint.cs
- Light.cs
- _AutoWebProxyScriptEngine.cs
- DesignerVerbToolStripMenuItem.cs
- InstalledFontCollection.cs
- ServiceBusyException.cs
- MessageSecurityOverHttp.cs
- NativeMethods.cs
- RadioButtonDesigner.cs
- ConstraintEnumerator.cs
- HttpPostProtocolImporter.cs
- InputLanguageEventArgs.cs
- TrustManagerPromptUI.cs
- SqlCommand.cs
- WebConfigurationFileMap.cs
- EntitySetBase.cs
- HashMembershipCondition.cs
- ListControl.cs
- XmlElementCollection.cs
- XmlTypeAttribute.cs
- NetNamedPipeSecurity.cs
- WizardDesigner.cs
- InfoCardUIAgent.cs
- KeyedCollection.cs
- XPathQilFactory.cs
- SingleAnimation.cs
- WorkerRequest.cs
- WindowsListViewItemCheckBox.cs
- UriSection.cs
- ProviderCollection.cs
- SchemaCreator.cs