Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / wpf / src / Framework / System / Windows / Controls / ProgressBar.cs / 1 / ProgressBar.cs
//----------------------------------------------------------------------------
// File: ProgressBar.cs
//
// Description:
// Implementation of ProgressBar control.
//
// History:
// 06/28/2004 - t-sergin - Created
//
// Copyright (C) 2004 by Microsoft Corporation. All rights reserved.
//
//---------------------------------------------------------------------------
using System;
using System.Collections.Specialized;
using System.Threading;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using MS.Internal.KnownBoxes;
namespace System.Windows.Controls
{
///
/// The ProgressBar class
///
///
[TemplatePart(Name = "PART_Track", Type = typeof(FrameworkElement))]
[TemplatePart(Name = "PART_Indicator", Type = typeof(FrameworkElement))]
public class ProgressBar : RangeBase
{
#region Constructors
static ProgressBar()
{
FocusableProperty.OverrideMetadata(typeof(ProgressBar), new FrameworkPropertyMetadata(BooleanBoxes.FalseBox));
DefaultStyleKeyProperty.OverrideMetadata(typeof(ProgressBar), new FrameworkPropertyMetadata(typeof(ProgressBar)));
_dType = DependencyObjectType.FromSystemTypeInternal(typeof(ProgressBar));
// Set default to 100.0
RangeBase.MaximumProperty.OverrideMetadata(typeof(ProgressBar), new FrameworkPropertyMetadata(100.0));
}
///
/// Instantiates a new instance of Progressbar without Dispatcher.
///
public ProgressBar() : base()
{
}
#endregion Constructors
#region Properties
///
/// The DependencyProperty for the IsIndeterminate property.
/// Flags: none
/// DefaultValue: false
///
public static readonly DependencyProperty IsIndeterminateProperty =
DependencyProperty.Register(
"IsIndeterminate",
typeof(bool),
typeof(ProgressBar),
new FrameworkPropertyMetadata(
false,
new PropertyChangedCallback(OnIsIndeterminateChanged)));
///
/// Determines if ProgressBar shows actual values (false)
/// or generic, continuous progress feedback (true).
///
///
public bool IsIndeterminate
{
get { return (bool) GetValue(IsIndeterminateProperty); }
set { SetValue(IsIndeterminateProperty, value); }
}
///
/// Called when IsIndeterminateProperty is changed on "d".
///
private static void OnIsIndeterminateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ProgressBar progressBar = (ProgressBar)d;
// Invalidate automation peer
ProgressBarAutomationPeer peer = UIElementAutomationPeer.FromElement(progressBar) as ProgressBarAutomationPeer;
if (peer != null)
{
peer.InvalidatePeer();
}
progressBar.SetProgressBarIndicatorLength();
}
///
/// DependencyProperty for property.
///
public static readonly DependencyProperty OrientationProperty =
DependencyProperty.Register(
"Orientation",
typeof(Orientation),
typeof(ProgressBar),
new FrameworkPropertyMetadata(
Orientation.Horizontal,
FrameworkPropertyMetadataOptions.AffectsMeasure),
new ValidateValueCallback(IsValidOrientation));
///
/// Specifies orientation of the ProgressBar.
///
public Orientation Orientation
{
get { return (Orientation) GetValue(OrientationProperty); }
set { SetValue(OrientationProperty, value); }
}
internal static bool IsValidOrientation(object o)
{
Orientation value = (Orientation)o;
return value == Orientation.Horizontal
|| value == Orientation.Vertical;
}
#endregion Properties
#region Event Handler
// Set the width/height of the contract parts
private void SetProgressBarIndicatorLength()
{
if (_track != null && _indicator != null)
{
double min = Minimum;
double max = Maximum;
double val = Value;
// When indeterminate or maximum == minimum, have the indicator stretch the
// whole length of track
double percent = IsIndeterminate || max == min ? 1.0 : (val - min) / (max - min);
if (Orientation == Orientation.Horizontal)
{
_indicator.Width = percent * _track.ActualWidth;
}
else // Oriention == Vertical
{
_indicator.Height = percent * _track.ActualHeight;
}
}
}
#endregion
#region Method Overrides
///
/// Creates AutomationPeer ( )
///
protected override AutomationPeer OnCreateAutomationPeer()
{
return new ProgressBarAutomationPeer(this);
}
///
/// This method is invoked when the Value property changes.
/// ProgressBar updates its style parts when Value changes.
///
/// The old value of the Value property.
/// The new value of the Value property.
protected override void OnValueChanged(double oldValue, double newValue)
{
base.OnValueChanged(oldValue, newValue);
SetProgressBarIndicatorLength();
}
///
/// Called when the Template's tree has been generated
///
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
if (_track != null)
{
_track.SizeChanged -= OnTrackSizeChanged;
}
_track = GetTemplateChild(TrackTemplateName) as FrameworkElement;
_indicator = GetTemplateChild(IndicatorTemplateName) as FrameworkElement;
if (_track != null)
{
_track.SizeChanged += OnTrackSizeChanged;
}
}
private void OnTrackSizeChanged(object sender, SizeChangedEventArgs e)
{
SetProgressBarIndicatorLength();
}
#endregion
#region Data
private const string TrackTemplateName = "PART_Track";
private const string IndicatorTemplateName = "PART_Indicator";
private FrameworkElement _track;
private FrameworkElement _indicator;
#endregion Data
#region DTypeThemeStyleKey
// Returns the DependencyObjectType for the registered ThemeStyleKey'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.
//----------------------------------------------------------------------------
// File: ProgressBar.cs
//
// Description:
// Implementation of ProgressBar control.
//
// History:
// 06/28/2004 - t-sergin - Created
//
// Copyright (C) 2004 by Microsoft Corporation. All rights reserved.
//
//---------------------------------------------------------------------------
using System;
using System.Collections.Specialized;
using System.Threading;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using MS.Internal.KnownBoxes;
namespace System.Windows.Controls
{
///
/// The ProgressBar class
///
///
[TemplatePart(Name = "PART_Track", Type = typeof(FrameworkElement))]
[TemplatePart(Name = "PART_Indicator", Type = typeof(FrameworkElement))]
public class ProgressBar : RangeBase
{
#region Constructors
static ProgressBar()
{
FocusableProperty.OverrideMetadata(typeof(ProgressBar), new FrameworkPropertyMetadata(BooleanBoxes.FalseBox));
DefaultStyleKeyProperty.OverrideMetadata(typeof(ProgressBar), new FrameworkPropertyMetadata(typeof(ProgressBar)));
_dType = DependencyObjectType.FromSystemTypeInternal(typeof(ProgressBar));
// Set default to 100.0
RangeBase.MaximumProperty.OverrideMetadata(typeof(ProgressBar), new FrameworkPropertyMetadata(100.0));
}
///
/// Instantiates a new instance of Progressbar without Dispatcher.
///
public ProgressBar() : base()
{
}
#endregion Constructors
#region Properties
///
/// The DependencyProperty for the IsIndeterminate property.
/// Flags: none
/// DefaultValue: false
///
public static readonly DependencyProperty IsIndeterminateProperty =
DependencyProperty.Register(
"IsIndeterminate",
typeof(bool),
typeof(ProgressBar),
new FrameworkPropertyMetadata(
false,
new PropertyChangedCallback(OnIsIndeterminateChanged)));
///
/// Determines if ProgressBar shows actual values (false)
/// or generic, continuous progress feedback (true).
///
///
public bool IsIndeterminate
{
get { return (bool) GetValue(IsIndeterminateProperty); }
set { SetValue(IsIndeterminateProperty, value); }
}
///
/// Called when IsIndeterminateProperty is changed on "d".
///
private static void OnIsIndeterminateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ProgressBar progressBar = (ProgressBar)d;
// Invalidate automation peer
ProgressBarAutomationPeer peer = UIElementAutomationPeer.FromElement(progressBar) as ProgressBarAutomationPeer;
if (peer != null)
{
peer.InvalidatePeer();
}
progressBar.SetProgressBarIndicatorLength();
}
///
/// DependencyProperty for property.
///
public static readonly DependencyProperty OrientationProperty =
DependencyProperty.Register(
"Orientation",
typeof(Orientation),
typeof(ProgressBar),
new FrameworkPropertyMetadata(
Orientation.Horizontal,
FrameworkPropertyMetadataOptions.AffectsMeasure),
new ValidateValueCallback(IsValidOrientation));
///
/// Specifies orientation of the ProgressBar.
///
public Orientation Orientation
{
get { return (Orientation) GetValue(OrientationProperty); }
set { SetValue(OrientationProperty, value); }
}
internal static bool IsValidOrientation(object o)
{
Orientation value = (Orientation)o;
return value == Orientation.Horizontal
|| value == Orientation.Vertical;
}
#endregion Properties
#region Event Handler
// Set the width/height of the contract parts
private void SetProgressBarIndicatorLength()
{
if (_track != null && _indicator != null)
{
double min = Minimum;
double max = Maximum;
double val = Value;
// When indeterminate or maximum == minimum, have the indicator stretch the
// whole length of track
double percent = IsIndeterminate || max == min ? 1.0 : (val - min) / (max - min);
if (Orientation == Orientation.Horizontal)
{
_indicator.Width = percent * _track.ActualWidth;
}
else // Oriention == Vertical
{
_indicator.Height = percent * _track.ActualHeight;
}
}
}
#endregion
#region Method Overrides
///
/// Creates AutomationPeer ( )
///
protected override AutomationPeer OnCreateAutomationPeer()
{
return new ProgressBarAutomationPeer(this);
}
///
/// This method is invoked when the Value property changes.
/// ProgressBar updates its style parts when Value changes.
///
/// The old value of the Value property.
/// The new value of the Value property.
protected override void OnValueChanged(double oldValue, double newValue)
{
base.OnValueChanged(oldValue, newValue);
SetProgressBarIndicatorLength();
}
///
/// Called when the Template's tree has been generated
///
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
if (_track != null)
{
_track.SizeChanged -= OnTrackSizeChanged;
}
_track = GetTemplateChild(TrackTemplateName) as FrameworkElement;
_indicator = GetTemplateChild(IndicatorTemplateName) as FrameworkElement;
if (_track != null)
{
_track.SizeChanged += OnTrackSizeChanged;
}
}
private void OnTrackSizeChanged(object sender, SizeChangedEventArgs e)
{
SetProgressBarIndicatorLength();
}
#endregion
#region Data
private const string TrackTemplateName = "PART_Track";
private const string IndicatorTemplateName = "PART_Indicator";
private FrameworkElement _track;
private FrameworkElement _indicator;
#endregion Data
#region DTypeThemeStyleKey
// Returns the DependencyObjectType for the registered ThemeStyleKey'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
- StyleSheet.cs
- TextEditorMouse.cs
- ClientConvert.cs
- MasterPageBuildProvider.cs
- SerializationHelper.cs
- RequestCacheValidator.cs
- UnsafeNativeMethods.cs
- UseManagedPresentationElement.cs
- DesignColumn.cs
- UTF32Encoding.cs
- OnOperation.cs
- LinearGradientBrush.cs
- DrawListViewColumnHeaderEventArgs.cs
- AssemblyAttributes.cs
- DefaultSection.cs
- ClickablePoint.cs
- DataList.cs
- TextServicesCompartmentEventSink.cs
- XpsDocument.cs
- ConfigXmlSignificantWhitespace.cs
- AuthenticationModuleElement.cs
- ExpressionBuilder.cs
- HtmlElementEventArgs.cs
- CategoryAttribute.cs
- NonClientArea.cs
- OrderedDictionary.cs
- TripleDES.cs
- Events.cs
- HelpKeywordAttribute.cs
- SkewTransform.cs
- DirectoryObjectSecurity.cs
- PassportAuthenticationModule.cs
- Internal.cs
- PathSegment.cs
- WebConvert.cs
- ObjectTypeMapping.cs
- XomlCompiler.cs
- UshortList2.cs
- MSAANativeProvider.cs
- DefaultBinder.cs
- ProtocolsSection.cs
- MatrixIndependentAnimationStorage.cs
- CommonProperties.cs
- BitmapEffectGeneralTransform.cs
- DataSourceXmlElementAttribute.cs
- TemplateParser.cs
- Point4DValueSerializer.cs
- StylusEditingBehavior.cs
- StylusPointProperty.cs
- FileNotFoundException.cs
- ClientApiGenerator.cs
- SqlCharStream.cs
- RegexWorker.cs
- AsynchronousChannel.cs
- TdsParserSafeHandles.cs
- Popup.cs
- KnownIds.cs
- TableProviderWrapper.cs
- UnmanagedBitmapWrapper.cs
- CustomErrorsSectionWrapper.cs
- EntityProxyFactory.cs
- OutputCacheSettingsSection.cs
- CodeDelegateInvokeExpression.cs
- ServerType.cs
- CommonDialog.cs
- EventLogWatcher.cs
- CodeBinaryOperatorExpression.cs
- SwitchDesigner.xaml.cs
- GenericRootAutomationPeer.cs
- WsdlBuildProvider.cs
- ReadOnlyMetadataCollection.cs
- RunInstallerAttribute.cs
- ImageIndexConverter.cs
- UnmanagedMemoryStreamWrapper.cs
- HyperLinkStyle.cs
- CacheMemory.cs
- IndexerNameAttribute.cs
- BindingOperations.cs
- UseLicense.cs
- EmptyElement.cs
- XmlEnumAttribute.cs
- ObjectQuery.cs
- PasswordTextContainer.cs
- XmlMapping.cs
- TransactedBatchContext.cs
- StylusButton.cs
- SecurityUniqueId.cs
- CodeTypeParameterCollection.cs
- ScriptReferenceBase.cs
- ReadOnlyPropertyMetadata.cs
- PackageProperties.cs
- FontResourceCache.cs
- AnnotationComponentManager.cs
- Hashtable.cs
- Rect.cs
- SqlNode.cs
- Odbc32.cs
- ClickablePoint.cs
- DetailsViewModeEventArgs.cs
- HtmlInputCheckBox.cs