Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Framework / System / Windows / Controls / DataGridHyperlinkColumn.cs / 1305600 / DataGridHyperlinkColumn.cs
//---------------------------------------------------------------------------- // // Copyright (C) Microsoft Corporation. All rights reserved. // //--------------------------------------------------------------------------- using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Windows; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; namespace System.Windows.Controls { ////// A column that displays a hyperlink. /// public class DataGridHyperlinkColumn : DataGridBoundColumn { static DataGridHyperlinkColumn() { ElementStyleProperty.OverrideMetadata(typeof(DataGridHyperlinkColumn), new FrameworkPropertyMetadata(DefaultElementStyle)); EditingElementStyleProperty.OverrideMetadata(typeof(DataGridHyperlinkColumn), new FrameworkPropertyMetadata(DefaultEditingElementStyle)); } #region Hyperlink Column Properties ////// Dependecy property for TargetName Property /// public static readonly DependencyProperty TargetNameProperty = Hyperlink.TargetNameProperty.AddOwner( typeof(DataGridHyperlinkColumn), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(DataGridColumn.NotifyPropertyChangeForRefreshContent))); ////// The property which determines the target name of the hyperlink /// public string TargetName { get { return (string)GetValue(TargetNameProperty); } set { SetValue(TargetNameProperty, value); } } ////// The binding to the content to be display under hyperlink /// public BindingBase ContentBinding { get { return _contentBinding; } set { if (_contentBinding != value) { BindingBase oldValue = _contentBinding; _contentBinding = value; OnContentBindingChanged(oldValue, value); } } } ////// Called when ContentBinding changes. /// ////// Default implementation notifies the DataGrid and its subtree about the change. /// /// The old binding. /// The new binding. protected virtual void OnContentBindingChanged(BindingBase oldBinding, BindingBase newBinding) { NotifyPropertyChanged("ContentBinding"); } ////// Try applying ContentBinding. If it doesnt work out apply Binding. /// /// /// private void ApplyContentBinding(DependencyObject target, DependencyProperty property) { if (ContentBinding != null) { BindingOperations.SetBinding(target, property, ContentBinding); } else if (Binding != null) { BindingOperations.SetBinding(target, property, Binding); } else { BindingOperations.ClearBinding(target, property); } } #endregion #region Property Changed Handler ////// Override which rebuilds the cell's visual tree for ContentBinding change /// and Modifies Hyperlink for TargetName change /// /// /// protected internal override void RefreshCellContent(FrameworkElement element, string propertyName) { DataGridCell cell = element as DataGridCell; if (cell != null && !cell.IsEditing) { if (string.Compare(propertyName, "ContentBinding", StringComparison.Ordinal) == 0) { cell.BuildVisualTree(); } else if (string.Compare(propertyName, "TargetName", StringComparison.Ordinal) == 0) { TextBlock outerBlock = cell.Content as TextBlock; if (outerBlock != null && outerBlock.Inlines.Count > 0) { Hyperlink link = outerBlock.Inlines.FirstInline as Hyperlink; if (link != null) { link.TargetName = TargetName; } } } } else { base.RefreshCellContent(element, propertyName); } } #endregion #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 { return DataGridTextColumn.DefaultElementStyle; } } ////// The default value of the EditingElementStyle property. /// This value can be used as the BasedOn for new styles. /// public static Style DefaultEditingElementStyle { get { return DataGridTextColumn.DefaultEditingElementStyle; } } #endregion #region Element Generation ////// Creates the visual tree for cells. /// protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem) { TextBlock outerBlock = new TextBlock(); Hyperlink link = new Hyperlink(); InlineUIContainer inlineContainer = new InlineUIContainer(); ContentPresenter innerContentPresenter = new ContentPresenter(); outerBlock.Inlines.Add(link); link.Inlines.Add(inlineContainer); inlineContainer.Child = innerContentPresenter; link.TargetName = TargetName; ApplyStyle(/* isEditing = */ false, /* defaultToElementStyle = */ false, outerBlock); ApplyBinding(link, Hyperlink.NavigateUriProperty); ApplyContentBinding(innerContentPresenter, ContentPresenter.ContentProperty); return outerBlock; } ////// Creates the visual tree for cells. /// protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem) { TextBox textBox = new TextBox(); ApplyStyle(/* isEditing = */ true, /* defaultToElementStyle = */ false, textBox); ApplyBinding(textBox, TextBox.TextProperty); return textBox; } #endregion #region Editing ////// 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) { TextBox textBox = editingElement as TextBox; if (textBox != null) { textBox.Focus(); string originalValue = textBox.Text; TextCompositionEventArgs textArgs = editingEventArgs as TextCompositionEventArgs; if (textArgs != null) { // If text input started the edit, then replace the text with what was typed. string inputText = textArgs.Text; textBox.Text = inputText; // Place the caret after the end of the text. textBox.Select(inputText.Length, 0); } else { // If something else started the edit, then select the text textBox.SelectAll(); } return originalValue; } return null; } internal override void OnInput(InputEventArgs e) { // Text input will start an edit. // Escape is meant to be for CancelEdit. But DataGrid // may not handle KeyDown event for Escape if nothing // is cancelable. Such KeyDown if unhandled by others // will ultimately get promoted to TextInput and be handled // here. But BeginEdit on escape could be confusing to the user. // Hence escape key is special case and BeginEdit is performed if // there is atleast one non espace key character. if (DataGridHelper.HasNonEscapeCharacters(e as TextCompositionEventArgs)) { BeginEdit(e); } } #endregion #region Data private BindingBase _contentBinding = null; #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.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Windows; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; namespace System.Windows.Controls { ////// A column that displays a hyperlink. /// public class DataGridHyperlinkColumn : DataGridBoundColumn { static DataGridHyperlinkColumn() { ElementStyleProperty.OverrideMetadata(typeof(DataGridHyperlinkColumn), new FrameworkPropertyMetadata(DefaultElementStyle)); EditingElementStyleProperty.OverrideMetadata(typeof(DataGridHyperlinkColumn), new FrameworkPropertyMetadata(DefaultEditingElementStyle)); } #region Hyperlink Column Properties ////// Dependecy property for TargetName Property /// public static readonly DependencyProperty TargetNameProperty = Hyperlink.TargetNameProperty.AddOwner( typeof(DataGridHyperlinkColumn), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(DataGridColumn.NotifyPropertyChangeForRefreshContent))); ////// The property which determines the target name of the hyperlink /// public string TargetName { get { return (string)GetValue(TargetNameProperty); } set { SetValue(TargetNameProperty, value); } } ////// The binding to the content to be display under hyperlink /// public BindingBase ContentBinding { get { return _contentBinding; } set { if (_contentBinding != value) { BindingBase oldValue = _contentBinding; _contentBinding = value; OnContentBindingChanged(oldValue, value); } } } ////// Called when ContentBinding changes. /// ////// Default implementation notifies the DataGrid and its subtree about the change. /// /// The old binding. /// The new binding. protected virtual void OnContentBindingChanged(BindingBase oldBinding, BindingBase newBinding) { NotifyPropertyChanged("ContentBinding"); } ////// Try applying ContentBinding. If it doesnt work out apply Binding. /// /// /// private void ApplyContentBinding(DependencyObject target, DependencyProperty property) { if (ContentBinding != null) { BindingOperations.SetBinding(target, property, ContentBinding); } else if (Binding != null) { BindingOperations.SetBinding(target, property, Binding); } else { BindingOperations.ClearBinding(target, property); } } #endregion #region Property Changed Handler ////// Override which rebuilds the cell's visual tree for ContentBinding change /// and Modifies Hyperlink for TargetName change /// /// /// protected internal override void RefreshCellContent(FrameworkElement element, string propertyName) { DataGridCell cell = element as DataGridCell; if (cell != null && !cell.IsEditing) { if (string.Compare(propertyName, "ContentBinding", StringComparison.Ordinal) == 0) { cell.BuildVisualTree(); } else if (string.Compare(propertyName, "TargetName", StringComparison.Ordinal) == 0) { TextBlock outerBlock = cell.Content as TextBlock; if (outerBlock != null && outerBlock.Inlines.Count > 0) { Hyperlink link = outerBlock.Inlines.FirstInline as Hyperlink; if (link != null) { link.TargetName = TargetName; } } } } else { base.RefreshCellContent(element, propertyName); } } #endregion #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 { return DataGridTextColumn.DefaultElementStyle; } } ////// The default value of the EditingElementStyle property. /// This value can be used as the BasedOn for new styles. /// public static Style DefaultEditingElementStyle { get { return DataGridTextColumn.DefaultEditingElementStyle; } } #endregion #region Element Generation ////// Creates the visual tree for cells. /// protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem) { TextBlock outerBlock = new TextBlock(); Hyperlink link = new Hyperlink(); InlineUIContainer inlineContainer = new InlineUIContainer(); ContentPresenter innerContentPresenter = new ContentPresenter(); outerBlock.Inlines.Add(link); link.Inlines.Add(inlineContainer); inlineContainer.Child = innerContentPresenter; link.TargetName = TargetName; ApplyStyle(/* isEditing = */ false, /* defaultToElementStyle = */ false, outerBlock); ApplyBinding(link, Hyperlink.NavigateUriProperty); ApplyContentBinding(innerContentPresenter, ContentPresenter.ContentProperty); return outerBlock; } ////// Creates the visual tree for cells. /// protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem) { TextBox textBox = new TextBox(); ApplyStyle(/* isEditing = */ true, /* defaultToElementStyle = */ false, textBox); ApplyBinding(textBox, TextBox.TextProperty); return textBox; } #endregion #region Editing ////// 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) { TextBox textBox = editingElement as TextBox; if (textBox != null) { textBox.Focus(); string originalValue = textBox.Text; TextCompositionEventArgs textArgs = editingEventArgs as TextCompositionEventArgs; if (textArgs != null) { // If text input started the edit, then replace the text with what was typed. string inputText = textArgs.Text; textBox.Text = inputText; // Place the caret after the end of the text. textBox.Select(inputText.Length, 0); } else { // If something else started the edit, then select the text textBox.SelectAll(); } return originalValue; } return null; } internal override void OnInput(InputEventArgs e) { // Text input will start an edit. // Escape is meant to be for CancelEdit. But DataGrid // may not handle KeyDown event for Escape if nothing // is cancelable. Such KeyDown if unhandled by others // will ultimately get promoted to TextInput and be handled // here. But BeginEdit on escape could be confusing to the user. // Hence escape key is special case and BeginEdit is performed if // there is atleast one non espace key character. if (DataGridHelper.HasNonEscapeCharacters(e as TextCompositionEventArgs)) { BeginEdit(e); } } #endregion #region Data private BindingBase _contentBinding = null; #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
- SeekableReadStream.cs
- DefaultHttpHandler.cs
- EditorAttribute.cs
- MachineKeySection.cs
- ThreadBehavior.cs
- DataObjectFieldAttribute.cs
- SqlDataAdapter.cs
- ProjectionPruner.cs
- TextServicesManager.cs
- HttpCapabilitiesEvaluator.cs
- LayeredChannelFactory.cs
- TcpHostedTransportConfiguration.cs
- CancellationScope.cs
- RotateTransform.cs
- CssTextWriter.cs
- InputProviderSite.cs
- HostingPreferredMapPath.cs
- DataBoundControl.cs
- XmlSchemaAny.cs
- ParameterBinding.cs
- TemplateControlCodeDomTreeGenerator.cs
- CheckBox.cs
- AddingNewEventArgs.cs
- XmlCharCheckingWriter.cs
- HttpWebRequestElement.cs
- SqlDuplicator.cs
- LambdaCompiler.Logical.cs
- DLinqColumnProvider.cs
- SiteIdentityPermission.cs
- ComplexTypeEmitter.cs
- DbConnectionClosed.cs
- SQLConvert.cs
- XPathCompileException.cs
- ZipIOCentralDirectoryFileHeader.cs
- RoutedPropertyChangedEventArgs.cs
- VirtualizingPanel.cs
- Dispatcher.cs
- BevelBitmapEffect.cs
- ReceiveDesigner.xaml.cs
- PropertyTabChangedEvent.cs
- InternalConfigEventArgs.cs
- DesignerLoader.cs
- ETagAttribute.cs
- Assert.cs
- FileSystemInfo.cs
- Rect3D.cs
- IsolatedStorageFilePermission.cs
- DispatcherEventArgs.cs
- ServiceHostingEnvironment.cs
- RepeatButton.cs
- StylusPointDescription.cs
- BroadcastEventHelper.cs
- WinFormsUtils.cs
- FrameworkContextData.cs
- BitmapDecoder.cs
- ErrorLog.cs
- XmlExtensionFunction.cs
- Error.cs
- DateTimeConstantAttribute.cs
- QilInvokeEarlyBound.cs
- XmlResolver.cs
- DbLambda.cs
- SystemEvents.cs
- CompressEmulationStream.cs
- FontStretch.cs
- bindurihelper.cs
- SHA512Managed.cs
- DaylightTime.cs
- DataGridViewRowPostPaintEventArgs.cs
- RegularExpressionValidator.cs
- Table.cs
- NamespaceCollection.cs
- AnimationLayer.cs
- TransformCryptoHandle.cs
- ITreeGenerator.cs
- DbConnectionInternal.cs
- PowerEase.cs
- BasicHttpBindingElement.cs
- CriticalFileToken.cs
- TextInfo.cs
- HtmlFormWrapper.cs
- ApplicationSettingsBase.cs
- StoragePropertyMapping.cs
- WebPartDeleteVerb.cs
- SqlBooleanizer.cs
- PresentationAppDomainManager.cs
- PropertyOverridesDialog.cs
- DataGridViewElement.cs
- PointValueSerializer.cs
- EditorPartCollection.cs
- DataGridRowAutomationPeer.cs
- WindowsSysHeader.cs
- StorageMappingItemCollection.cs
- EventLogStatus.cs
- ToggleButtonAutomationPeer.cs
- PipelineModuleStepContainer.cs
- RootProfilePropertySettingsCollection.cs
- DataKeyArray.cs
- FileUpload.cs
- PasswordValidationException.cs