Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Framework / System / Windows / Controls / Primitives / DatePickerTextBox.cs / 1305600 / DatePickerTextBox.cs
//----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//---------------------------------------------------------------------------
using System.Diagnostics;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
namespace System.Windows.Controls.Primitives
{
///
/// DatePickerTextBox is a specialized form of TextBox which displays custom visuals when its contents are empty
///
[TemplatePart(Name = DatePickerTextBox.ElementContentName, Type = typeof(ContentControl))]
public sealed partial class DatePickerTextBox : TextBox
{
#region Constants
private const string ElementContentName = "PART_Watermark";
#endregion
#region Data
private ContentControl elementContent;
#endregion
#region Constructor
///
/// Static constructor
///
static DatePickerTextBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(DatePickerTextBox), new FrameworkPropertyMetadata(typeof(DatePickerTextBox)));
TextProperty.OverrideMetadata(typeof(DatePickerTextBox), new FrameworkPropertyMetadata(OnVisualStatePropertyChanged));
}
///
/// Initializes a new instance of the class.
///
public DatePickerTextBox()
{
this.SetCurrentValue(WatermarkProperty, SR.Get(SRID.DatePickerTextBox_DefaultWatermarkText));
this.Loaded += OnLoaded;
this.IsEnabledChanged += new DependencyPropertyChangedEventHandler(OnDatePickerTextBoxIsEnabledChanged);
}
#endregion
#region Public Properties
#region Watermark
///
/// Watermark dependency property
///
internal static readonly DependencyProperty WatermarkProperty = DependencyProperty.Register(
"Watermark", typeof(object), typeof(DatePickerTextBox), new PropertyMetadata(OnWatermarkPropertyChanged));
///
/// Watermark content
///
/// The watermark.
internal object Watermark
{
get { return (object)GetValue(WatermarkProperty); }
set { SetValue(WatermarkProperty, value); }
}
#endregion
#endregion Public Properties
#region Protected
///
/// Called when template is applied to the control.
///
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
elementContent = ExtractTemplatePart(ElementContentName);
// We dont want to expose watermark property as public yet, because there
// is a good chance in future that the implementation will change when
// a WatermarkTextBox control gets implemented. This is mostly to
// mimc SL. Hence setting the binding in code rather than in control template.
if (elementContent != null)
{
Binding watermarkBinding = new Binding("Watermark");
watermarkBinding.Source = this;
elementContent.SetBinding(ContentControl.ContentProperty, watermarkBinding);
}
OnWatermarkChanged();
}
protected override void OnGotFocus(RoutedEventArgs e)
{
base.OnGotFocus(e);
if (IsEnabled)
{
if (!string.IsNullOrEmpty(this.Text))
{
Select(0, this.Text.Length);
}
}
}
#endregion Protected
#region Private
private void OnLoaded(object sender, RoutedEventArgs e)
{
ApplyTemplate();
}
///
/// Change to the correct visual state for the textbox.
///
///
/// true to use transitions when updating the visual state, false to
/// snap directly to the new visual state.
///
internal override void ChangeVisualState(bool useTransitions)
{
base.ChangeVisualState(useTransitions);
// Update the WatermarkStates group
if (this.Watermark != null && string.IsNullOrEmpty(this.Text))
{
VisualStates.GoToState(this, useTransitions, VisualStates.StateWatermarked, VisualStates.StateUnwatermarked);
}
else
{
VisualStates.GoToState(this, useTransitions, VisualStates.StateUnwatermarked);
}
}
private T ExtractTemplatePart(string partName) where T : DependencyObject
{
DependencyObject obj = GetTemplateChild(partName);
return ExtractTemplatePart(partName, obj);
}
private static T ExtractTemplatePart(string partName, DependencyObject obj) where T : DependencyObject
{
Debug.Assert(
obj == null || typeof(T).IsInstanceOfType(obj),
string.Format(CultureInfo.InvariantCulture, SR.Get(SRID.DatePickerTextBox_TemplatePartIsOfIncorrectType), partName, typeof(T).Name));
return obj as T;
}
///
/// Called when the IsEnabled property changes.
///
/// Sender object
/// Property changed args
private void OnDatePickerTextBoxIsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
Debug.Assert(e.NewValue is bool);
bool isEnabled = (bool)e.NewValue;
SetCurrentValueInternal(IsReadOnlyProperty, MS.Internal.KnownBoxes.BooleanBoxes.Box(!isEnabled));
}
private void OnWatermarkChanged()
{
if (elementContent != null)
{
Control watermarkControl = this.Watermark as Control;
if (watermarkControl != null)
{
watermarkControl.IsTabStop = false;
watermarkControl.IsHitTestVisible = false;
}
}
}
///
/// Called when watermark property is changed.
///
/// The sender.
/// The instance containing the event data.
private static void OnWatermarkPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
DatePickerTextBox datePickerTextBox = sender as DatePickerTextBox;
Debug.Assert(datePickerTextBox != null, "The source is not an instance of a DatePickerTextBox!");
datePickerTextBox.OnWatermarkChanged();
datePickerTextBox.UpdateVisualState();
}
#endregion Private
}
}
// 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.Diagnostics;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
namespace System.Windows.Controls.Primitives
{
///
/// DatePickerTextBox is a specialized form of TextBox which displays custom visuals when its contents are empty
///
[TemplatePart(Name = DatePickerTextBox.ElementContentName, Type = typeof(ContentControl))]
public sealed partial class DatePickerTextBox : TextBox
{
#region Constants
private const string ElementContentName = "PART_Watermark";
#endregion
#region Data
private ContentControl elementContent;
#endregion
#region Constructor
///
/// Static constructor
///
static DatePickerTextBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(DatePickerTextBox), new FrameworkPropertyMetadata(typeof(DatePickerTextBox)));
TextProperty.OverrideMetadata(typeof(DatePickerTextBox), new FrameworkPropertyMetadata(OnVisualStatePropertyChanged));
}
///
/// Initializes a new instance of the class.
///
public DatePickerTextBox()
{
this.SetCurrentValue(WatermarkProperty, SR.Get(SRID.DatePickerTextBox_DefaultWatermarkText));
this.Loaded += OnLoaded;
this.IsEnabledChanged += new DependencyPropertyChangedEventHandler(OnDatePickerTextBoxIsEnabledChanged);
}
#endregion
#region Public Properties
#region Watermark
///
/// Watermark dependency property
///
internal static readonly DependencyProperty WatermarkProperty = DependencyProperty.Register(
"Watermark", typeof(object), typeof(DatePickerTextBox), new PropertyMetadata(OnWatermarkPropertyChanged));
///
/// Watermark content
///
/// The watermark.
internal object Watermark
{
get { return (object)GetValue(WatermarkProperty); }
set { SetValue(WatermarkProperty, value); }
}
#endregion
#endregion Public Properties
#region Protected
///
/// Called when template is applied to the control.
///
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
elementContent = ExtractTemplatePart(ElementContentName);
// We dont want to expose watermark property as public yet, because there
// is a good chance in future that the implementation will change when
// a WatermarkTextBox control gets implemented. This is mostly to
// mimc SL. Hence setting the binding in code rather than in control template.
if (elementContent != null)
{
Binding watermarkBinding = new Binding("Watermark");
watermarkBinding.Source = this;
elementContent.SetBinding(ContentControl.ContentProperty, watermarkBinding);
}
OnWatermarkChanged();
}
protected override void OnGotFocus(RoutedEventArgs e)
{
base.OnGotFocus(e);
if (IsEnabled)
{
if (!string.IsNullOrEmpty(this.Text))
{
Select(0, this.Text.Length);
}
}
}
#endregion Protected
#region Private
private void OnLoaded(object sender, RoutedEventArgs e)
{
ApplyTemplate();
}
///
/// Change to the correct visual state for the textbox.
///
///
/// true to use transitions when updating the visual state, false to
/// snap directly to the new visual state.
///
internal override void ChangeVisualState(bool useTransitions)
{
base.ChangeVisualState(useTransitions);
// Update the WatermarkStates group
if (this.Watermark != null && string.IsNullOrEmpty(this.Text))
{
VisualStates.GoToState(this, useTransitions, VisualStates.StateWatermarked, VisualStates.StateUnwatermarked);
}
else
{
VisualStates.GoToState(this, useTransitions, VisualStates.StateUnwatermarked);
}
}
private T ExtractTemplatePart(string partName) where T : DependencyObject
{
DependencyObject obj = GetTemplateChild(partName);
return ExtractTemplatePart(partName, obj);
}
private static T ExtractTemplatePart(string partName, DependencyObject obj) where T : DependencyObject
{
Debug.Assert(
obj == null || typeof(T).IsInstanceOfType(obj),
string.Format(CultureInfo.InvariantCulture, SR.Get(SRID.DatePickerTextBox_TemplatePartIsOfIncorrectType), partName, typeof(T).Name));
return obj as T;
}
///
/// Called when the IsEnabled property changes.
///
/// Sender object
/// Property changed args
private void OnDatePickerTextBoxIsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
Debug.Assert(e.NewValue is bool);
bool isEnabled = (bool)e.NewValue;
SetCurrentValueInternal(IsReadOnlyProperty, MS.Internal.KnownBoxes.BooleanBoxes.Box(!isEnabled));
}
private void OnWatermarkChanged()
{
if (elementContent != null)
{
Control watermarkControl = this.Watermark as Control;
if (watermarkControl != null)
{
watermarkControl.IsTabStop = false;
watermarkControl.IsHitTestVisible = false;
}
}
}
///
/// Called when watermark property is changed.
///
/// The sender.
/// The instance containing the event data.
private static void OnWatermarkPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
DatePickerTextBox datePickerTextBox = sender as DatePickerTextBox;
Debug.Assert(datePickerTextBox != null, "The source is not an instance of a DatePickerTextBox!");
datePickerTextBox.OnWatermarkChanged();
datePickerTextBox.UpdateVisualState();
}
#endregion Private
}
}
// 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
- XmlDomTextWriter.cs
- WebPartVerbsEventArgs.cs
- HybridDictionary.cs
- DataGridViewRowDividerDoubleClickEventArgs.cs
- DocumentSequence.cs
- BinHexEncoder.cs
- EmptyImpersonationContext.cs
- IPAddressCollection.cs
- DataServiceExpressionVisitor.cs
- FilePrompt.cs
- RequestCacheValidator.cs
- OdbcConnectionPoolProviderInfo.cs
- ErrorRuntimeConfig.cs
- ControlValuePropertyAttribute.cs
- AppSettingsReader.cs
- SiteMapPathDesigner.cs
- SimpleBitVector32.cs
- MenuItemStyleCollectionEditor.cs
- _SpnDictionary.cs
- QilReference.cs
- InvalidEnumArgumentException.cs
- ObjectDataSourceStatusEventArgs.cs
- Model3DGroup.cs
- GlyphingCache.cs
- ChildrenQuery.cs
- GridEntryCollection.cs
- TextViewSelectionProcessor.cs
- WorkflowLayouts.cs
- _SingleItemRequestCache.cs
- DataGridViewColumnTypePicker.cs
- CodeDomSerializerException.cs
- DesignerTransaction.cs
- Convert.cs
- TextEditorLists.cs
- ServiceDescription.cs
- Interfaces.cs
- MessageQueuePermissionEntryCollection.cs
- HttpCacheVary.cs
- CodeIdentifier.cs
- OracleEncoding.cs
- OutputCacheProviderCollection.cs
- FileSecurity.cs
- SessionPageStateSection.cs
- COM2Properties.cs
- BindingOperations.cs
- HashRepartitionStream.cs
- DataBoundControl.cs
- Stacktrace.cs
- EnterpriseServicesHelper.cs
- Empty.cs
- ProcessThreadDesigner.cs
- sitestring.cs
- RTLAwareMessageBox.cs
- Calendar.cs
- Timeline.cs
- UriExt.cs
- PropertyGeneratedEventArgs.cs
- Cursor.cs
- MembershipPasswordException.cs
- FileSystemWatcher.cs
- DataRelation.cs
- KeyEventArgs.cs
- ClientScriptManager.cs
- SizeChangedEventArgs.cs
- ToolStripItemEventArgs.cs
- EventToken.cs
- RemotingServices.cs
- webbrowsersite.cs
- DotNetATv1WindowsLogEntryDeserializer.cs
- BindUriHelper.cs
- ListMarkerLine.cs
- InfocardChannelParameter.cs
- ParserStreamGeometryContext.cs
- BinaryReader.cs
- BatchServiceHost.cs
- DataServiceContext.cs
- TileBrush.cs
- PropertyBuilder.cs
- XmlSchemaDatatype.cs
- Matrix3D.cs
- FixedSOMLineRanges.cs
- _DomainName.cs
- WebScriptMetadataMessage.cs
- CodeArgumentReferenceExpression.cs
- ClientApiGenerator.cs
- ping.cs
- DataGridViewComponentPropertyGridSite.cs
- XmlLanguageConverter.cs
- KeyNotFoundException.cs
- ScriptBehaviorDescriptor.cs
- _FixedSizeReader.cs
- HierarchicalDataBoundControlAdapter.cs
- WebPartHelpVerb.cs
- TemplateField.cs
- PolicyValidationException.cs
- SettingsPropertyIsReadOnlyException.cs
- DocumentApplicationJournalEntry.cs
- AnnotationAdorner.cs
- TreeNodeCollection.cs
- ConfigurationElement.cs