Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Framework / System / Windows / Controls / DataGridTemplateColumn.cs / 1305600 / DataGridTemplateColumn.cs
//---------------------------------------------------------------------------- // // Copyright (C) Microsoft Corporation. All rights reserved. // //--------------------------------------------------------------------------- using System; using System.ComponentModel; using System.Diagnostics; using System.Windows; using System.Windows.Data; namespace System.Windows.Controls { ////// A column definition that allows a developer to specify specific /// editing and non-editing templates. /// public class DataGridTemplateColumn : DataGridColumn { #region Constructors static DataGridTemplateColumn() { CanUserSortProperty.OverrideMetadata( typeof(DataGridTemplateColumn), new FrameworkPropertyMetadata(null, new CoerceValueCallback(OnCoerceTemplateColumnCanUserSort))); SortMemberPathProperty.OverrideMetadata( typeof(DataGridTemplateColumn), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnTemplateColumnSortMemberPathChanged))); } #endregion #region Auto Sort private static void OnTemplateColumnSortMemberPathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DataGridTemplateColumn column = (DataGridTemplateColumn)d; column.CoerceValue(CanUserSortProperty); } private static object OnCoerceTemplateColumnCanUserSort(DependencyObject d, object baseValue) { DataGridTemplateColumn templateColumn = (DataGridTemplateColumn)d; if (string.IsNullOrEmpty(templateColumn.SortMemberPath)) { return false; } return DataGridColumn.OnCoerceCanUserSort(d, baseValue); } #endregion #region Templates ////// A template describing how to display data for a cell in this column. /// public DataTemplate CellTemplate { get { return (DataTemplate)GetValue(CellTemplateProperty); } set { SetValue(CellTemplateProperty, value); } } ////// The DependencyProperty representing the CellTemplate property. /// public static readonly DependencyProperty CellTemplateProperty = DependencyProperty.Register( "CellTemplate", typeof(DataTemplate), typeof(DataGridTemplateColumn), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(DataGridColumn.NotifyPropertyChangeForRefreshContent))); ////// A template selector describing how to display data for a cell in this column. /// public DataTemplateSelector CellTemplateSelector { get { return (DataTemplateSelector)GetValue(CellTemplateSelectorProperty); } set { SetValue(CellTemplateSelectorProperty, value); } } ////// The DependencyProperty representing the CellTemplateSelector property. /// public static readonly DependencyProperty CellTemplateSelectorProperty = DependencyProperty.Register( "CellTemplateSelector", typeof(DataTemplateSelector), typeof(DataGridTemplateColumn), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(DataGridColumn.NotifyPropertyChangeForRefreshContent))); ////// A template describing how to display data for a cell /// that is being edited in this column. /// public DataTemplate CellEditingTemplate { get { return (DataTemplate)GetValue(CellEditingTemplateProperty); } set { SetValue(CellEditingTemplateProperty, value); } } ////// The DependencyProperty representing the CellEditingTemplate /// public static readonly DependencyProperty CellEditingTemplateProperty = DependencyProperty.Register( "CellEditingTemplate", typeof(DataTemplate), typeof(DataGridTemplateColumn), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(DataGridColumn.NotifyPropertyChangeForRefreshContent))); ////// A template selector describing how to display data for a cell /// that is being edited in this column. /// public DataTemplateSelector CellEditingTemplateSelector { get { return (DataTemplateSelector)GetValue(CellEditingTemplateSelectorProperty); } set { SetValue(CellEditingTemplateSelectorProperty, value); } } ////// The DependencyProperty representing the CellEditingTemplateSelector /// public static readonly DependencyProperty CellEditingTemplateSelectorProperty = DependencyProperty.Register( "CellEditingTemplateSelector", typeof(DataTemplateSelector), typeof(DataGridTemplateColumn), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(DataGridColumn.NotifyPropertyChangeForRefreshContent))); ////// Returns either the specified CellTemplate or CellEditingTemplate. /// CellTemplate is returned if CellEditingTemplate is null. /// /// Whether the editing template is requested. private DataTemplate ChooseCellTemplate(bool isEditing) { DataTemplate template = null; if (isEditing) { template = CellEditingTemplate; } if (template == null) { template = CellTemplate; } return template; } ////// Returns either the specified CellTemplateSelector or CellEditingTemplateSelector. /// CellTemplateSelector is returned if CellEditingTemplateSelector is null. /// /// Whether the editing template selector is requested. private DataTemplateSelector ChooseCellTemplateSelector(bool isEditing) { DataTemplateSelector templateSelector = null; if (isEditing) { templateSelector = CellEditingTemplateSelector; } if (templateSelector == null) { templateSelector = CellTemplateSelector; } return templateSelector; } #endregion #region Visual Tree Generation ////// Creates the visual tree that will become the content of a cell. /// /// Whether the editing version is being requested. /// The data item for the cell. /// The cell container that will receive the tree. private FrameworkElement LoadTemplateContent(bool isEditing, object dataItem, DataGridCell cell) { DataTemplate template = ChooseCellTemplate(isEditing); DataTemplateSelector templateSelector = ChooseCellTemplateSelector(isEditing); if (template != null || templateSelector != null) { ContentPresenter contentPresenter = new ContentPresenter(); BindingOperations.SetBinding(contentPresenter, ContentPresenter.ContentProperty, new Binding()); contentPresenter.ContentTemplate = template; contentPresenter.ContentTemplateSelector = templateSelector; return contentPresenter; } return null; } ////// Creates the visual tree that will become the content of a cell. /// protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem) { return LoadTemplateContent(/* isEditing = */ false, dataItem, cell); } ////// Creates the visual tree that will become the content of a cell. /// protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem) { return LoadTemplateContent(/* isEditing = */ true, dataItem, cell); } #endregion #region Property Changed Handler ////// Override which handles property /// change for template properties /// /// /// protected internal override void RefreshCellContent(FrameworkElement element, string propertyName) { DataGridCell cell = element as DataGridCell; if (cell != null) { bool isCellEditing = cell.IsEditing; if ((!isCellEditing && ((string.Compare(propertyName, "CellTemplate", StringComparison.Ordinal) == 0) || (string.Compare(propertyName, "CellTemplateSelector", StringComparison.Ordinal) == 0))) || (isCellEditing && ((string.Compare(propertyName, "CellEditingTemplate", StringComparison.Ordinal) == 0) || (string.Compare(propertyName, "CellEditingTemplateSelector", StringComparison.Ordinal) == 0)))) { cell.BuildVisualTree(); return; } } base.RefreshCellContent(element, propertyName); } #endregion } } // 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.ComponentModel; using System.Diagnostics; using System.Windows; using System.Windows.Data; namespace System.Windows.Controls { ////// A column definition that allows a developer to specify specific /// editing and non-editing templates. /// public class DataGridTemplateColumn : DataGridColumn { #region Constructors static DataGridTemplateColumn() { CanUserSortProperty.OverrideMetadata( typeof(DataGridTemplateColumn), new FrameworkPropertyMetadata(null, new CoerceValueCallback(OnCoerceTemplateColumnCanUserSort))); SortMemberPathProperty.OverrideMetadata( typeof(DataGridTemplateColumn), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnTemplateColumnSortMemberPathChanged))); } #endregion #region Auto Sort private static void OnTemplateColumnSortMemberPathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DataGridTemplateColumn column = (DataGridTemplateColumn)d; column.CoerceValue(CanUserSortProperty); } private static object OnCoerceTemplateColumnCanUserSort(DependencyObject d, object baseValue) { DataGridTemplateColumn templateColumn = (DataGridTemplateColumn)d; if (string.IsNullOrEmpty(templateColumn.SortMemberPath)) { return false; } return DataGridColumn.OnCoerceCanUserSort(d, baseValue); } #endregion #region Templates ////// A template describing how to display data for a cell in this column. /// public DataTemplate CellTemplate { get { return (DataTemplate)GetValue(CellTemplateProperty); } set { SetValue(CellTemplateProperty, value); } } ////// The DependencyProperty representing the CellTemplate property. /// public static readonly DependencyProperty CellTemplateProperty = DependencyProperty.Register( "CellTemplate", typeof(DataTemplate), typeof(DataGridTemplateColumn), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(DataGridColumn.NotifyPropertyChangeForRefreshContent))); ////// A template selector describing how to display data for a cell in this column. /// public DataTemplateSelector CellTemplateSelector { get { return (DataTemplateSelector)GetValue(CellTemplateSelectorProperty); } set { SetValue(CellTemplateSelectorProperty, value); } } ////// The DependencyProperty representing the CellTemplateSelector property. /// public static readonly DependencyProperty CellTemplateSelectorProperty = DependencyProperty.Register( "CellTemplateSelector", typeof(DataTemplateSelector), typeof(DataGridTemplateColumn), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(DataGridColumn.NotifyPropertyChangeForRefreshContent))); ////// A template describing how to display data for a cell /// that is being edited in this column. /// public DataTemplate CellEditingTemplate { get { return (DataTemplate)GetValue(CellEditingTemplateProperty); } set { SetValue(CellEditingTemplateProperty, value); } } ////// The DependencyProperty representing the CellEditingTemplate /// public static readonly DependencyProperty CellEditingTemplateProperty = DependencyProperty.Register( "CellEditingTemplate", typeof(DataTemplate), typeof(DataGridTemplateColumn), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(DataGridColumn.NotifyPropertyChangeForRefreshContent))); ////// A template selector describing how to display data for a cell /// that is being edited in this column. /// public DataTemplateSelector CellEditingTemplateSelector { get { return (DataTemplateSelector)GetValue(CellEditingTemplateSelectorProperty); } set { SetValue(CellEditingTemplateSelectorProperty, value); } } ////// The DependencyProperty representing the CellEditingTemplateSelector /// public static readonly DependencyProperty CellEditingTemplateSelectorProperty = DependencyProperty.Register( "CellEditingTemplateSelector", typeof(DataTemplateSelector), typeof(DataGridTemplateColumn), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(DataGridColumn.NotifyPropertyChangeForRefreshContent))); ////// Returns either the specified CellTemplate or CellEditingTemplate. /// CellTemplate is returned if CellEditingTemplate is null. /// /// Whether the editing template is requested. private DataTemplate ChooseCellTemplate(bool isEditing) { DataTemplate template = null; if (isEditing) { template = CellEditingTemplate; } if (template == null) { template = CellTemplate; } return template; } ////// Returns either the specified CellTemplateSelector or CellEditingTemplateSelector. /// CellTemplateSelector is returned if CellEditingTemplateSelector is null. /// /// Whether the editing template selector is requested. private DataTemplateSelector ChooseCellTemplateSelector(bool isEditing) { DataTemplateSelector templateSelector = null; if (isEditing) { templateSelector = CellEditingTemplateSelector; } if (templateSelector == null) { templateSelector = CellTemplateSelector; } return templateSelector; } #endregion #region Visual Tree Generation ////// Creates the visual tree that will become the content of a cell. /// /// Whether the editing version is being requested. /// The data item for the cell. /// The cell container that will receive the tree. private FrameworkElement LoadTemplateContent(bool isEditing, object dataItem, DataGridCell cell) { DataTemplate template = ChooseCellTemplate(isEditing); DataTemplateSelector templateSelector = ChooseCellTemplateSelector(isEditing); if (template != null || templateSelector != null) { ContentPresenter contentPresenter = new ContentPresenter(); BindingOperations.SetBinding(contentPresenter, ContentPresenter.ContentProperty, new Binding()); contentPresenter.ContentTemplate = template; contentPresenter.ContentTemplateSelector = templateSelector; return contentPresenter; } return null; } ////// Creates the visual tree that will become the content of a cell. /// protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem) { return LoadTemplateContent(/* isEditing = */ false, dataItem, cell); } ////// Creates the visual tree that will become the content of a cell. /// protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem) { return LoadTemplateContent(/* isEditing = */ true, dataItem, cell); } #endregion #region Property Changed Handler ////// Override which handles property /// change for template properties /// /// /// protected internal override void RefreshCellContent(FrameworkElement element, string propertyName) { DataGridCell cell = element as DataGridCell; if (cell != null) { bool isCellEditing = cell.IsEditing; if ((!isCellEditing && ((string.Compare(propertyName, "CellTemplate", StringComparison.Ordinal) == 0) || (string.Compare(propertyName, "CellTemplateSelector", StringComparison.Ordinal) == 0))) || (isCellEditing && ((string.Compare(propertyName, "CellEditingTemplate", StringComparison.Ordinal) == 0) || (string.Compare(propertyName, "CellEditingTemplateSelector", StringComparison.Ordinal) == 0)))) { cell.BuildVisualTree(); return; } } base.RefreshCellContent(element, propertyName); } #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
- ConnectionManagementSection.cs
- ConsumerConnectionPointCollection.cs
- DurableEnlistmentState.cs
- CommandManager.cs
- WhiteSpaceTrimStringConverter.cs
- DateTimeFormatInfoScanner.cs
- SettingsAttributeDictionary.cs
- DataViewManager.cs
- RepeatInfo.cs
- Transactions.cs
- Matrix3DConverter.cs
- ColorMap.cs
- infer.cs
- StrongNameIdentityPermission.cs
- SqlConnectionPoolGroupProviderInfo.cs
- FindSimilarActivitiesVerb.cs
- Helper.cs
- ScrollBarAutomationPeer.cs
- NotFiniteNumberException.cs
- NavigationWindow.cs
- XmlObjectSerializerContext.cs
- ParameterEditorUserControl.cs
- IgnoreDeviceFilterElementCollection.cs
- PrinterResolution.cs
- FlatButtonAppearance.cs
- Model3DCollection.cs
- ThaiBuddhistCalendar.cs
- StateRuntime.cs
- DayRenderEvent.cs
- ProjectionPruner.cs
- RequestNavigateEventArgs.cs
- RegistrationServices.cs
- TextDecorationCollectionConverter.cs
- FontNamesConverter.cs
- AbsoluteQuery.cs
- ConnectionPoint.cs
- serverconfig.cs
- UdpMessageProperty.cs
- FormViewRow.cs
- ComponentChangingEvent.cs
- CommonDialog.cs
- TempFiles.cs
- FrameworkElementFactory.cs
- HtmlInputText.cs
- CheckPair.cs
- DiscoveryServerProtocol.cs
- ModuleConfigurationInfo.cs
- WSAddressing10ProblemHeaderQNameFault.cs
- ThreadInterruptedException.cs
- FontDialog.cs
- SqlTypesSchemaImporter.cs
- WebEvents.cs
- ParagraphResult.cs
- PersistenceProviderFactory.cs
- DeriveBytes.cs
- TypeConverterHelper.cs
- _Rfc2616CacheValidators.cs
- loginstatus.cs
- XmlFormatReaderGenerator.cs
- NetStream.cs
- DoubleLink.cs
- entityreference_tresulttype.cs
- EmbeddedMailObject.cs
- AccessedThroughPropertyAttribute.cs
- Point3DAnimationBase.cs
- StaticSiteMapProvider.cs
- CodeConditionStatement.cs
- Scripts.cs
- WebServiceMethodData.cs
- CellTreeNodeVisitors.cs
- DeferredElementTreeState.cs
- InheritedPropertyChangedEventArgs.cs
- WorkflowTerminatedException.cs
- PageEventArgs.cs
- TransportBindingElementImporter.cs
- ConnectionConsumerAttribute.cs
- AuthenticationException.cs
- TileBrush.cs
- DeviceContexts.cs
- RequestFactory.cs
- ManipulationInertiaStartingEventArgs.cs
- HandoffBehavior.cs
- OrthographicCamera.cs
- AggregateNode.cs
- EmptyStringExpandableObjectConverter.cs
- XhtmlCssHandler.cs
- ParagraphResult.cs
- EdmSchemaError.cs
- EntitySqlQueryCacheKey.cs
- NamespaceEmitter.cs
- CoordinationService.cs
- MailDefinition.cs
- FocusChangedEventArgs.cs
- StylusEventArgs.cs
- HttpConfigurationSystem.cs
- DeferredSelectedIndexReference.cs
- Hashtable.cs
- QilFactory.cs
- ExcCanonicalXml.cs
- UrlMapping.cs