Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / wpf / src / Framework / System / Windows / Controls / MenuScrollingVisibilityConverter.cs / 1 / MenuScrollingVisibilityConverter.cs
//----------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
// Description: Used to show or hide arrow buttons in scrolling menus.
//
//---------------------------------------------------------------------------
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Automation.Provider;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media;
using MS.Internal;
namespace System.Windows.Controls
{
///
/// Data binding converter to handle the visibility of repeat buttons in scrolling menus.
///
public sealed class MenuScrollingVisibilityConverter : IMultiValueConverter
{
///
/// Convert a value. Called when moving a value from source to target.
///
/// values as produced by source binding
/// target type
/// converter parameter
/// culture information
///
/// Converted value.
///
/// System.Windows.DependencyProperty.UnsetValue may be returned to indicate that
/// the converter produced no value and that the fallback (if available)
/// or default value should be used instead.
///
/// Binding.DoNothing may be returned to indicate that the binding
/// should not transfer the value or use the fallback or default value.
///
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
//
// Parameter Validation
//
Type doubleType = typeof(double);
if (parameter == null ||
values == null ||
values.Length != 4 ||
values[0] == null ||
values[1] == null ||
values[2] == null ||
values[3] == null ||
!typeof(Visibility).IsAssignableFrom(values[0].GetType()) ||
!doubleType.IsAssignableFrom(values[1].GetType()) ||
!doubleType.IsAssignableFrom(values[2].GetType()) ||
!doubleType.IsAssignableFrom(values[3].GetType()) )
{
return DependencyProperty.UnsetValue;
}
Type paramType = parameter.GetType();
if (!(doubleType.IsAssignableFrom(paramType) || typeof(string).IsAssignableFrom(paramType)))
{
return DependencyProperty.UnsetValue;
}
//
// Conversion
//
// If the scroll bar should be visible, then so should our buttons
Visibility computedVerticalScrollBarVisibility = (Visibility)values[0];
if (computedVerticalScrollBarVisibility == Visibility.Visible)
{
double target;
if (parameter is string)
{
target = Double.Parse(((string)parameter), NumberFormatInfo.InvariantInfo);
}
else
{
target = (double)parameter;
}
double verticalOffset = (double)values[1];
double extentHeight = (double)values[2];
double viewportHeight = (double)values[3];
if (extentHeight != viewportHeight) // Avoid divide by 0
{
// Calculate the percent so that we can see if we are near the edge of the range
double percent = Math.Min(100.0, Math.Max(0.0, (verticalOffset * 100.0 / (extentHeight - viewportHeight))));
if (DoubleUtil.AreClose(percent, target))
{
// We are at the end of the range, so no need for this button to be shown
return Visibility.Collapsed;
}
}
return Visibility.Visible;
}
return Visibility.Collapsed;
}
///
/// Not Supported
///
/// value, as produced by target
/// target types
/// converter parameter
/// culture information
/// Nothing
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return new object[] { Binding.DoNothing };
}
}
}
// 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.
//
//
// Description: Used to show or hide arrow buttons in scrolling menus.
//
//---------------------------------------------------------------------------
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Automation.Provider;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media;
using MS.Internal;
namespace System.Windows.Controls
{
///
/// Data binding converter to handle the visibility of repeat buttons in scrolling menus.
///
public sealed class MenuScrollingVisibilityConverter : IMultiValueConverter
{
///
/// Convert a value. Called when moving a value from source to target.
///
/// values as produced by source binding
/// target type
/// converter parameter
/// culture information
///
/// Converted value.
///
/// System.Windows.DependencyProperty.UnsetValue may be returned to indicate that
/// the converter produced no value and that the fallback (if available)
/// or default value should be used instead.
///
/// Binding.DoNothing may be returned to indicate that the binding
/// should not transfer the value or use the fallback or default value.
///
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
//
// Parameter Validation
//
Type doubleType = typeof(double);
if (parameter == null ||
values == null ||
values.Length != 4 ||
values[0] == null ||
values[1] == null ||
values[2] == null ||
values[3] == null ||
!typeof(Visibility).IsAssignableFrom(values[0].GetType()) ||
!doubleType.IsAssignableFrom(values[1].GetType()) ||
!doubleType.IsAssignableFrom(values[2].GetType()) ||
!doubleType.IsAssignableFrom(values[3].GetType()) )
{
return DependencyProperty.UnsetValue;
}
Type paramType = parameter.GetType();
if (!(doubleType.IsAssignableFrom(paramType) || typeof(string).IsAssignableFrom(paramType)))
{
return DependencyProperty.UnsetValue;
}
//
// Conversion
//
// If the scroll bar should be visible, then so should our buttons
Visibility computedVerticalScrollBarVisibility = (Visibility)values[0];
if (computedVerticalScrollBarVisibility == Visibility.Visible)
{
double target;
if (parameter is string)
{
target = Double.Parse(((string)parameter), NumberFormatInfo.InvariantInfo);
}
else
{
target = (double)parameter;
}
double verticalOffset = (double)values[1];
double extentHeight = (double)values[2];
double viewportHeight = (double)values[3];
if (extentHeight != viewportHeight) // Avoid divide by 0
{
// Calculate the percent so that we can see if we are near the edge of the range
double percent = Math.Min(100.0, Math.Max(0.0, (verticalOffset * 100.0 / (extentHeight - viewportHeight))));
if (DoubleUtil.AreClose(percent, target))
{
// We are at the end of the range, so no need for this button to be shown
return Visibility.Collapsed;
}
}
return Visibility.Visible;
}
return Visibility.Collapsed;
}
///
/// Not Supported
///
/// value, as produced by target
/// target types
/// converter parameter
/// culture information
/// Nothing
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return new object[] { Binding.DoNothing };
}
}
}
// 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
- SiteOfOriginPart.cs
- MsmqActivation.cs
- AddInStore.cs
- TrackingDataItem.cs
- DesignerAdapterAttribute.cs
- AppDomainUnloadedException.cs
- HttpPostLocalhostServerProtocol.cs
- PropertyGridCommands.cs
- SSmlParser.cs
- FontConverter.cs
- LogicalChannel.cs
- ColumnTypeConverter.cs
- TracePayload.cs
- LicenseContext.cs
- KeyToListMap.cs
- mediaclock.cs
- SqlStatistics.cs
- WebRequest.cs
- HelpEvent.cs
- ReplacementText.cs
- DataObject.cs
- IPHostEntry.cs
- ReadOnlyObservableCollection.cs
- ReturnEventArgs.cs
- Pair.cs
- FileCodeGroup.cs
- SamlDelegatingWriter.cs
- CollectionViewSource.cs
- DataSourceSerializationException.cs
- BitmapEffectGroup.cs
- StorageEntitySetMapping.cs
- HandledEventArgs.cs
- UnauthorizedAccessException.cs
- StreamWriter.cs
- DataGridViewColumn.cs
- ControlsConfig.cs
- FrugalMap.cs
- GridViewAutoFormat.cs
- TemplateAction.cs
- DataGridViewBand.cs
- SubpageParagraph.cs
- SwitchDesigner.xaml.cs
- DbReferenceCollection.cs
- RuntimeWrappedException.cs
- FormViewDeleteEventArgs.cs
- SHA384.cs
- SerializableAttribute.cs
- MultipleViewProviderWrapper.cs
- XmlSchemaInclude.cs
- XamlSerializerUtil.cs
- XmlValueConverter.cs
- DocumentCollection.cs
- UInt16Converter.cs
- TypedElement.cs
- odbcmetadatafactory.cs
- HitTestFilterBehavior.cs
- GenericPrincipal.cs
- WaveHeader.cs
- DateTimePicker.cs
- CryptoProvider.cs
- MsmqReceiveParameters.cs
- XPathAxisIterator.cs
- MissingMemberException.cs
- GPPOINT.cs
- XmlWriter.cs
- DocumentProperties.cs
- SectionUpdates.cs
- EntityConnectionStringBuilderItem.cs
- Filter.cs
- InvokeProviderWrapper.cs
- XmlException.cs
- ContentPresenter.cs
- UnmanagedMemoryStream.cs
- UriTemplateCompoundPathSegment.cs
- QueryableDataSourceEditData.cs
- AppDomainManager.cs
- ComplexTypeEmitter.cs
- LicenseException.cs
- Rule.cs
- ServiceTimeoutsElement.cs
- PassportAuthenticationModule.cs
- ExpressionBindings.cs
- XmlHelper.cs
- ConnectionStringsExpressionBuilder.cs
- InfoCardKeyedHashAlgorithm.cs
- ValidationErrorCollection.cs
- SafeBitVector32.cs
- ThemeableAttribute.cs
- HwndSourceKeyboardInputSite.cs
- GacUtil.cs
- GeometryModel3D.cs
- GradientSpreadMethodValidation.cs
- SessionPageStateSection.cs
- AccessViolationException.cs
- ObjectDataSourceChooseTypePanel.cs
- Adorner.cs
- SafeBitVector32.cs
- NamedPipeDuplicateContext.cs
- MultiTrigger.cs
- FrameworkElement.cs