Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Framework / System / Windows / Controls / HeaderedContentControl.cs / 1305600 / HeaderedContentControl.cs
//----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//---------------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
using System.Windows.Threading;
using System.Windows.Media;
using MS.Utility;
using MS.Internal;
using MS.Internal.Controls;
using MS.Internal.Data;
using MS.Internal.KnownBoxes;
using MS.Internal.PresentationFramework;
namespace System.Windows.Controls
{
///
/// The base class for all controls that contain single content and have a header.
///
///
/// HeaderedContentControl adds Header, HasHeader, HeaderTemplate, and HeaderTemplateSelector features to a ContentControl.
///
[Localizability(LocalizationCategory.Text)] // can contain localizable text
public class HeaderedContentControl : ContentControl
{
#region Constructors
static HeaderedContentControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(HeaderedContentControl), new FrameworkPropertyMetadata(typeof(HeaderedContentControl)));
_dType = DependencyObjectType.FromSystemTypeInternal(typeof(HeaderedContentControl));
}
///
/// Default DependencyObject constructor
///
public HeaderedContentControl() : base()
{
}
#endregion
#region Properties
///
/// The DependencyProperty for the Header property.
/// Flags: None
/// Default Value: null
///
[CommonDependencyProperty]
public static readonly DependencyProperty HeaderProperty =
DependencyProperty.Register(
"Header",
typeof(object),
typeof(HeaderedContentControl),
new FrameworkPropertyMetadata(
(object) null,
new PropertyChangedCallback(OnHeaderChanged)));
///
/// Header is the data used to for the header of each item in the control.
///
[Bindable(true), Category("Content")]
[Localizability(LocalizationCategory.Label)]
public object Header
{
get { return GetValue(HeaderProperty); }
set { SetValue(HeaderProperty, value); }
}
///
/// Called when HeaderProperty is invalidated on "d."
///
private static void OnHeaderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
HeaderedContentControl ctrl = (HeaderedContentControl) d;
ctrl.SetValue(HasHeaderPropertyKey, (e.NewValue != null) ? BooleanBoxes.TrueBox : BooleanBoxes.FalseBox);
ctrl.OnHeaderChanged(e.OldValue, e.NewValue);
}
///
/// This method is invoked when the Header property changes.
///
/// The old value of the Header property.
/// The new value of the Header property.
protected virtual void OnHeaderChanged(object oldHeader, object newHeader)
{
RemoveLogicalChild(oldHeader);
AddLogicalChild(newHeader);
}
///
/// The key needed set a read-only property.
///
internal static readonly DependencyPropertyKey HasHeaderPropertyKey =
DependencyProperty.RegisterReadOnly(
"HasHeader",
typeof(bool),
typeof(HeaderedContentControl),
new FrameworkPropertyMetadata(BooleanBoxes.FalseBox));
///
/// The DependencyProperty for the HasHeader property.
/// Flags: None
/// Other: Read-Only
/// Default Value: false
///
[CommonDependencyProperty]
public static readonly DependencyProperty HasHeaderProperty =
HasHeaderPropertyKey.DependencyProperty;
///
/// True if Header is non-null, false otherwise.
///
[Bindable(false), Browsable(false)]
public bool HasHeader
{
get { return (bool) GetValue(HasHeaderProperty); }
}
///
/// The DependencyProperty for the HeaderTemplate property.
/// Flags: Can be used in style rules
/// Default Value: null
///
[CommonDependencyProperty]
public static readonly DependencyProperty HeaderTemplateProperty =
DependencyProperty.Register(
"HeaderTemplate",
typeof(DataTemplate),
typeof(HeaderedContentControl),
new FrameworkPropertyMetadata(
(DataTemplate) null,
new PropertyChangedCallback(OnHeaderTemplateChanged)));
///
/// HeaderTemplate is the template used to display the .
///
[Bindable(true), Category("Content")]
public DataTemplate HeaderTemplate
{
get { return (DataTemplate) GetValue(HeaderTemplateProperty); }
set { SetValue(HeaderTemplateProperty, value); }
}
///
/// Called when HeaderTemplateProperty is invalidated on "d."
///
private static void OnHeaderTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
HeaderedContentControl ctrl = (HeaderedContentControl)d;
ctrl.OnHeaderTemplateChanged((DataTemplate) e.OldValue, (DataTemplate) e.NewValue);
}
///
/// This method is invoked when the HeaderTemplate property changes.
///
/// The old value of the HeaderTemplate property.
/// The new value of the HeaderTemplate property.
protected virtual void OnHeaderTemplateChanged(DataTemplate oldHeaderTemplate, DataTemplate newHeaderTemplate)
{
Helper.CheckTemplateAndTemplateSelector("Header", HeaderTemplateProperty, HeaderTemplateSelectorProperty, this);
}
///
/// The DependencyProperty for the HeaderTemplateSelector property.
/// Flags: none
/// Default Value: null
///
[CommonDependencyProperty]
public static readonly DependencyProperty HeaderTemplateSelectorProperty =
DependencyProperty.Register(
"HeaderTemplateSelector",
typeof(DataTemplateSelector),
typeof(HeaderedContentControl),
new FrameworkPropertyMetadata(
(DataTemplateSelector) null,
new PropertyChangedCallback(OnHeaderTemplateSelectorChanged)));
///
/// HeaderTemplateSelector allows the application writer to provide custom logic
/// for choosing the template used to display the .
///
///
/// This property is ignored if is set.
///
[Bindable(true), Category("Content")]
public DataTemplateSelector HeaderTemplateSelector
{
get { return (DataTemplateSelector) GetValue(HeaderTemplateSelectorProperty); }
set { SetValue(HeaderTemplateSelectorProperty, value); }
}
///
/// Called when HeaderTemplateSelectorProperty is invalidated on "d."
///
private static void OnHeaderTemplateSelectorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
HeaderedContentControl ctrl = (HeaderedContentControl) d;
ctrl.OnHeaderTemplateSelectorChanged((DataTemplateSelector) e.OldValue, (DataTemplateSelector) e.NewValue);
}
///
/// This method is invoked when the HeaderTemplateSelector property changes.
///
/// The old value of the HeaderTemplateSelector property.
/// The new value of the HeaderTemplateSelector property.
protected virtual void OnHeaderTemplateSelectorChanged(DataTemplateSelector oldHeaderTemplateSelector, DataTemplateSelector newHeaderTemplateSelector)
{
Helper.CheckTemplateAndTemplateSelector("Header", HeaderTemplateProperty, HeaderTemplateSelectorProperty, this);
}
///
/// The DependencyProperty for the HeaderStringFormat property.
/// Flags: None
/// Default Value: null
///
[CommonDependencyProperty]
public static readonly DependencyProperty HeaderStringFormatProperty =
DependencyProperty.Register(
"HeaderStringFormat",
typeof(String),
typeof(HeaderedContentControl),
new FrameworkPropertyMetadata(
(String) null,
new PropertyChangedCallback(OnHeaderStringFormatChanged)));
///
/// HeaderStringFormat is the format used to display the header content as a string.
/// This arises only when no template is available.
///
[Bindable(true), CustomCategory("Content")]
public String HeaderStringFormat
{
get { return (String) GetValue(HeaderStringFormatProperty); }
set { SetValue(HeaderStringFormatProperty, value); }
}
///
/// Called when HeaderStringFormatProperty is invalidated on "d."
///
private static void OnHeaderStringFormatChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
HeaderedContentControl ctrl = (HeaderedContentControl)d;
ctrl.OnHeaderStringFormatChanged((String) e.OldValue, (String) e.NewValue);
}
///
/// This method is invoked when the HeaderStringFormat property changes.
///
/// The old value of the HeaderStringFormat property.
/// The new value of the HeaderStringFormat property.
protected virtual void OnHeaderStringFormatChanged(String oldHeaderStringFormat, String newHeaderStringFormat)
{
}
#endregion
#region LogicalTree
///
/// Returns enumerator to logical children
///
protected internal override IEnumerator LogicalChildren
{
get
{
object header = Header;
if (HeaderIsNotLogical || header == null)
{
return base.LogicalChildren;
}
return new HeaderedContentModelTreeEnumerator(this, ContentIsNotLogical ? null : Content, header);
}
}
#endregion
#region Internal Methods
///
/// Gives a string representation of this object.
///
///
internal override string GetPlainText()
{
return ContentControl.ContentObjectToString(Header);
}
///
/// Indicates whether Header should be a logical child or not.
///
internal bool HeaderIsNotLogical
{
get { return ReadControlFlag(ControlBoolFlags.HeaderIsNotLogical); }
set { WriteControlFlag(ControlBoolFlags.HeaderIsNotLogical, value); }
}
///
/// Indicates whether Header is a data item
///
internal bool HeaderIsItem
{
get { return ReadControlFlag(ControlBoolFlags.HeaderIsItem); }
set { WriteControlFlag(ControlBoolFlags.HeaderIsItem, value); }
}
///
/// Prepare to display the item.
///
internal void PrepareHeaderedContentControl(object item,
DataTemplate itemTemplate,
DataTemplateSelector itemTemplateSelector,
string stringFormat)
{
if (item != this)
{
// don't treat Content as a logical child
ContentIsNotLogical = true;
HeaderIsNotLogical = true;
if (ContentIsItem || !HasNonDefaultValue(ContentProperty))
{
Content = item;
ContentIsItem = true;
}
// Visuals can't be placed in both Header and Content, but data can
if (!(item is Visual) && (HeaderIsItem || !HasNonDefaultValue(HeaderProperty)))
{
Header = item;
HeaderIsItem = true;
}
if (itemTemplate != null)
SetValue(HeaderTemplateProperty, itemTemplate);
if (itemTemplateSelector != null)
SetValue(HeaderTemplateSelectorProperty, itemTemplateSelector);
if (stringFormat != null)
SetValue(HeaderStringFormatProperty, stringFormat);
}
else
{
ContentIsNotLogical = false;
}
}
///
/// Undo the effect of PrepareHeaderedContentControl.
///
internal void ClearHeaderedContentControl(object item)
{
if (item != this)
{
if (ContentIsItem)
{
Content = BindingExpressionBase.DisconnectedItem;
}
if (HeaderIsItem)
{
Header = BindingExpressionBase.DisconnectedItem;
}
}
}
#endregion
#region Method Overrides
///
/// Gives a string representation of this object.
///
public override string ToString()
{
string typeText = this.GetType().ToString();
string headerText = String.Empty;
string contentText = String.Empty;
bool valuesDefined = false;
// Accessing Header's content may be thread sensitive
if (CheckAccess())
{
headerText = ContentControl.ContentObjectToString(Header);
contentText = ContentControl.ContentObjectToString(Content);
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)
{
headerText = ContentControl.ContentObjectToString(Header);
contentText = ContentControl.ContentObjectToString(Content);
valuesDefined = true;
return null;
}), null);
}
// If header and content text are defined
if (valuesDefined)
{
return SR.Get(SRID.ToStringFormatString_HeaderedContentControl, typeText, headerText, contentText);
}
// Not able to access the dispatcher
return typeText;
}
#endregion
#region DTypeThemeStyleKey
// Returns the DependencyObjectType for the registered DefaultStyleKey'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
- HashCodeCombiner.cs
- ProcessInputEventArgs.cs
- AccessorTable.cs
- CheckedListBox.cs
- Msmq4PoisonHandler.cs
- ToolboxItemCollection.cs
- CodeExpressionCollection.cs
- EnterpriseServicesHelper.cs
- CompressionTracing.cs
- TypeGeneratedEventArgs.cs
- SoapMessage.cs
- SyndicationContent.cs
- RSAOAEPKeyExchangeDeformatter.cs
- InitializeCorrelation.cs
- CultureSpecificStringDictionary.cs
- VsPropertyGrid.cs
- ExpandCollapseProviderWrapper.cs
- IResourceProvider.cs
- Form.cs
- RawStylusInputReport.cs
- NetworkInterface.cs
- RequestFactory.cs
- SqlInternalConnection.cs
- BitmapSizeOptions.cs
- StylusPointProperties.cs
- XPathDocumentIterator.cs
- WinInet.cs
- DataGridColumnHeaderCollection.cs
- SoapServerMethod.cs
- ParseHttpDate.cs
- StdValidatorsAndConverters.cs
- TimeSpanFormat.cs
- ObjectList.cs
- ShapingEngine.cs
- LookupNode.cs
- ParserStreamGeometryContext.cs
- ConfigurationErrorsException.cs
- TreeBuilder.cs
- BatchServiceHost.cs
- CodePropertyReferenceExpression.cs
- MultilineStringConverter.cs
- Line.cs
- DataGridTable.cs
- SecondaryIndex.cs
- WindowsScrollBarBits.cs
- OutputCacheSection.cs
- MetadataItem_Static.cs
- SectionRecord.cs
- FormClosedEvent.cs
- PortCache.cs
- ElementsClipboardData.cs
- InputLanguage.cs
- RankException.cs
- WebPartsPersonalizationAuthorization.cs
- CodePropertyReferenceExpression.cs
- StateManagedCollection.cs
- EncoderNLS.cs
- EntityType.cs
- AdapterUtil.cs
- ContextStack.cs
- CodeThrowExceptionStatement.cs
- PreviewPrintController.cs
- IdleTimeoutMonitor.cs
- IPEndPointCollection.cs
- WsdlEndpointConversionContext.cs
- InitializerFacet.cs
- DPCustomTypeDescriptor.cs
- OleDbConnection.cs
- ListViewHitTestInfo.cs
- TransformPattern.cs
- ProfileParameter.cs
- InkPresenterAutomationPeer.cs
- DateTimeOffsetAdapter.cs
- SynchronizationLockException.cs
- ArrangedElementCollection.cs
- XmlStringTable.cs
- DisplayNameAttribute.cs
- TraceUtils.cs
- XmlImplementation.cs
- SocketInformation.cs
- Splitter.cs
- WebBrowserUriTypeConverter.cs
- NetCodeGroup.cs
- TimerEventSubscriptionCollection.cs
- SqlProvider.cs
- Stylesheet.cs
- Transform3DGroup.cs
- ResXDataNode.cs
- StreamWithDictionary.cs
- FilterException.cs
- MetadataUtilsSmi.cs
- NotifyCollectionChangedEventArgs.cs
- ResXResourceWriter.cs
- UnmanagedMarshal.cs
- AliasedSlot.cs
- InternalUserCancelledException.cs
- CalendarDataBindingHandler.cs
- unsafeIndexingFilterStream.cs
- WebReferencesBuildProvider.cs
- Fault.cs