Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Framework / System / Windows / Controls / DataGridCheckBoxColumn.cs / 1305600 / DataGridCheckBoxColumn.cs
//---------------------------------------------------------------------------- // // Copyright (C) Microsoft Corporation. All rights reserved. // //--------------------------------------------------------------------------- using System; using System.ComponentModel; using System.Diagnostics; using System.Windows; using System.Windows.Input; namespace System.Windows.Controls { ////// A column that displays a check box. /// public class DataGridCheckBoxColumn : DataGridBoundColumn { static DataGridCheckBoxColumn() { ElementStyleProperty.OverrideMetadata(typeof(DataGridCheckBoxColumn), new FrameworkPropertyMetadata(DefaultElementStyle)); EditingElementStyleProperty.OverrideMetadata(typeof(DataGridCheckBoxColumn), new FrameworkPropertyMetadata(DefaultEditingElementStyle)); } #region Styles ////// The default value of the ElementStyle property. /// This value can be used as the BasedOn for new styles. /// public static Style DefaultElementStyle { get { if (_defaultElementStyle == null) { Style style = new Style(typeof(CheckBox)); // When not in edit mode, the end-user should not be able to toggle the state style.Setters.Add(new Setter(UIElement.IsHitTestVisibleProperty, false)); style.Setters.Add(new Setter(UIElement.FocusableProperty, false)); style.Setters.Add(new Setter(CheckBox.HorizontalAlignmentProperty, HorizontalAlignment.Center)); style.Setters.Add(new Setter(CheckBox.VerticalAlignmentProperty, VerticalAlignment.Top)); style.Seal(); _defaultElementStyle = style; } return _defaultElementStyle; } } ////// The default value of the EditingElementStyle property. /// This value can be used as the BasedOn for new styles. /// public static Style DefaultEditingElementStyle { get { if (_defaultEditingElementStyle == null) { Style style = new Style(typeof(CheckBox)); style.Setters.Add(new Setter(CheckBox.HorizontalAlignmentProperty, HorizontalAlignment.Center)); style.Setters.Add(new Setter(CheckBox.VerticalAlignmentProperty, VerticalAlignment.Top)); style.Seal(); _defaultEditingElementStyle = style; } return _defaultEditingElementStyle; } } #endregion #region Element Generation ////// Creates the visual tree for boolean based cells. /// protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem) { return GenerateCheckBox(/* isEditing = */ false, cell); } ////// Creates the visual tree for boolean based cells. /// protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem) { return GenerateCheckBox(/* isEditing = */ true, cell); } private CheckBox GenerateCheckBox(bool isEditing, DataGridCell cell) { CheckBox checkBox = (cell != null) ? (cell.Content as CheckBox) : null; if (checkBox == null) { checkBox = new CheckBox(); } checkBox.IsThreeState = IsThreeState; ApplyStyle(isEditing, /* defaultToElementStyle = */ true, checkBox); ApplyBinding(checkBox, CheckBox.IsCheckedProperty); return checkBox; } protected internal override void RefreshCellContent(FrameworkElement element, string propertyName) { DataGridCell cell = element as DataGridCell; if (cell != null && string.Compare(propertyName, "IsThreeState", StringComparison.Ordinal) == 0) { var checkBox = cell.Content as CheckBox; if (checkBox != null) { checkBox.IsThreeState = IsThreeState; } } else { base.RefreshCellContent(element, propertyName); } } #endregion #region Editing ////// The DependencyProperty for the IsThreeState property. /// Flags: None /// Default Value: false /// public static readonly DependencyProperty IsThreeStateProperty = CheckBox.IsThreeStateProperty.AddOwner( typeof(DataGridCheckBoxColumn), new FrameworkPropertyMetadata(false, DataGridColumn.NotifyPropertyChangeForRefreshContent)); ////// 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 /// public bool IsThreeState { get { return (bool)GetValue(IsThreeStateProperty); } set { SetValue(IsThreeStateProperty, value); } } ////// Called when a cell has just switched to edit mode. /// /// A reference to element returned by GenerateEditingElement. /// The event args of the input event that caused the cell to go into edit mode. May be null. ///The unedited value of the cell. protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs) { CheckBox checkBox = editingElement as CheckBox; if (checkBox != null) { checkBox.Focus(); bool? uneditedValue = checkBox.IsChecked; // If a click or a space key invoked the begin edit, then do an immediate toggle if ((IsMouseLeftButtonDown(editingEventArgs) && IsMouseOver(checkBox, editingEventArgs)) || IsSpaceKeyDown(editingEventArgs)) { checkBox.IsChecked = (uneditedValue != true); } return uneditedValue; } return (bool?) false; } internal override void OnInput(InputEventArgs e) { // Space key down will begin edit mode if (IsSpaceKeyDown(e)) { BeginEdit(e); } } private static bool IsMouseLeftButtonDown(RoutedEventArgs e) { MouseButtonEventArgs mouseArgs = e as MouseButtonEventArgs; return (mouseArgs != null) && (mouseArgs.ChangedButton == MouseButton.Left) && (mouseArgs.ButtonState == MouseButtonState.Pressed); } private static bool IsMouseOver(CheckBox checkBox, RoutedEventArgs e) { // This element is new, so the IsMouseOver property will not have been updated // yet, but there is enough information to do a hit-test. return checkBox.InputHitTest(((MouseButtonEventArgs)e).GetPosition(checkBox)) != null; } private static bool IsSpaceKeyDown(RoutedEventArgs e) { KeyEventArgs keyArgs = e as KeyEventArgs; return (keyArgs != null) && ((keyArgs.KeyStates & KeyStates.Down) == KeyStates.Down) && (keyArgs.Key == Key.Space); } #endregion #region Data private static Style _defaultElementStyle; private static Style _defaultEditingElementStyle; #endregion } } // 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
- NotFiniteNumberException.cs
- HandoffBehavior.cs
- ArgumentsParser.cs
- Padding.cs
- BlockExpression.cs
- Constraint.cs
- ActivityPropertyReference.cs
- CodeDirectiveCollection.cs
- Listbox.cs
- AutomationPropertyInfo.cs
- DataGridViewColumnStateChangedEventArgs.cs
- BaseResourcesBuildProvider.cs
- WebPartDisplayModeCollection.cs
- ExpressionDumper.cs
- SemanticTag.cs
- MetadataArtifactLoaderComposite.cs
- MouseBinding.cs
- ViewStateException.cs
- XPathException.cs
- BaseInfoTable.cs
- CodeSnippetExpression.cs
- NativeRightsManagementAPIsStructures.cs
- DivideByZeroException.cs
- SafeArrayRankMismatchException.cs
- WebPartConnectionsEventArgs.cs
- ActiveDocumentEvent.cs
- ListBindingConverter.cs
- SiteMapDataSource.cs
- SafeNativeMethods.cs
- VirtualPath.cs
- ValidatorCollection.cs
- ProfileEventArgs.cs
- DocumentSequenceHighlightLayer.cs
- HtmlElementErrorEventArgs.cs
- SynchronizationContext.cs
- AdapterUtil.cs
- WriterOutput.cs
- path.cs
- MemoryStream.cs
- CollectionType.cs
- OleDbEnumerator.cs
- ErrorActivity.cs
- TextPointer.cs
- NativeWrapper.cs
- FtpWebResponse.cs
- XmlChildNodes.cs
- ToolStripMenuItemDesigner.cs
- ExcludePathInfo.cs
- _SecureChannel.cs
- XmlEntity.cs
- KeyGestureValueSerializer.cs
- MemberInfoSerializationHolder.cs
- ScriptComponentDescriptor.cs
- CompilerState.cs
- Rect3D.cs
- Condition.cs
- AppDomainProtocolHandler.cs
- DesignerTextViewAdapter.cs
- ZipIOZip64EndOfCentralDirectoryLocatorBlock.cs
- UnsafeNativeMethods.cs
- DataSourceSelectArguments.cs
- WebPartZoneBase.cs
- ExtenderProvidedPropertyAttribute.cs
- Calendar.cs
- EDesignUtil.cs
- ChtmlMobileTextWriter.cs
- NavigationCommands.cs
- CryptoStream.cs
- OdbcException.cs
- CodeDesigner.cs
- Grid.cs
- GridItem.cs
- LazyLoadBehavior.cs
- Debug.cs
- HelpKeywordAttribute.cs
- BitmapInitialize.cs
- UxThemeWrapper.cs
- DataColumnChangeEvent.cs
- CompressEmulationStream.cs
- returneventsaver.cs
- DesignerActionKeyboardBehavior.cs
- XmlAnyElementAttributes.cs
- AspNetSynchronizationContext.cs
- RC2.cs
- XmlSchemaInferenceException.cs
- ProcessHostFactoryHelper.cs
- RootBrowserWindowProxy.cs
- Ops.cs
- SqlDesignerDataSourceView.cs
- Mouse.cs
- XamlTemplateSerializer.cs
- SHA512Managed.cs
- XsltQilFactory.cs
- FixedSOMLineRanges.cs
- InvokeBase.cs
- SqlUnionizer.cs
- RuleSettingsCollection.cs
- TableCellCollection.cs
- ConnectionPoint.cs
- DataGridItemAutomationPeer.cs