Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / Tools / System.Activities.Core.Presentation / System / Activities / Core / Presentation / CaseKeyBox.xaml.cs / 1305376 / CaseKeyBox.xaml.cs
//------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace System.Activities.Core.Presentation { using System; using System.Activities.Presentation; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Input; using System.Windows.Threading; partial class CaseKeyBox : UserControl, ICaseKeyBoxView { public static readonly DependencyProperty DisplayHintTextProperty = DependencyProperty.Register("DisplayHintText", typeof(bool), typeof(CaseKeyBox)); public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(object), typeof(CaseKeyBox), new PropertyMetadata(OnValueChanged)); public static readonly DependencyProperty ValueTypeProperty = DependencyProperty.Register("ValueType", typeof(Type), typeof(CaseKeyBox), new PropertyMetadata(OnValueTypeChanged)); public static RoutedEvent ValueCommittedEvent = EventManager.RegisterRoutedEvent("ValueCommitted", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(CaseKeyBox)); public static RoutedEvent EditCancelledEvent = EventManager.RegisterRoutedEvent("EditCancelled", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(CaseKeyBox)); public static readonly DependencyProperty CaseKeyValidationCallbackProperty = DependencyProperty.Register("CaseKeyValidationCallback", typeof(CaseKeyValidationCallbackDelegate), typeof(CaseKeyBox)); public static readonly DependencyProperty ErrorCallbackProperty = DependencyProperty.Register("ErrorCallback", typeof(Action), typeof(CaseKeyBox)); public static readonly DependencyProperty CommitExplicitlyProperty = DependencyProperty.Register("CommitExplicitly", typeof(bool), typeof(CaseKeyBox), new PropertyMetadata(false)); Control visibleBox; public CaseKeyBox() { this.ViewModel = new CaseKeyBoxViewModel(this); InitializeComponent(); } public event RoutedEventHandler ValueCommitted { add { AddHandler(ValueCommittedEvent, value); } remove { RemoveHandler(ValueCommittedEvent, value); } } public virtual void OnValueCommitted() { RoutedEventArgs args = new RoutedEventArgs(); args.RoutedEvent = ValueCommittedEvent; RaiseEvent(args); } public event RoutedEventHandler EditCancelled { add { AddHandler(EditCancelledEvent, value); } remove { RemoveHandler(EditCancelledEvent, value); } } public virtual void OnEditCancelled() { RoutedEventArgs args = new RoutedEventArgs(); args.RoutedEvent = EditCancelledEvent; RaiseEvent(args); } public CaseKeyValidationCallbackDelegate CaseKeyValidationCallback { get { return (CaseKeyValidationCallbackDelegate)GetValue(CaseKeyValidationCallbackProperty); } set { SetValue(CaseKeyValidationCallbackProperty, value); } } public Action ErrorCallback { get { return (Action )GetValue(ErrorCallbackProperty); } set { SetValue(ErrorCallbackProperty, value); } } public bool CommitExplicitly { get { return (bool)GetValue(CommitExplicitlyProperty); } set { SetValue(CommitExplicitlyProperty, value); } } void DisableKeyboardLostFocus() { if (this.visibleBox != null) { this.visibleBox.LostKeyboardFocus -= OnLostKeyboardFocus; } } void EnableKeyboardLostFocus() { if (!this.CommitExplicitly) { if (this.visibleBox != null) { this.visibleBox.LostKeyboardFocus += OnLostKeyboardFocus; } } } void ReportError(string errorMessage) { // Invoking error message box will cause LostFocus of the control. // Thus we need to disable LostFocus first and then add the handlers back. DisableKeyboardLostFocus(); ErrorReporting.ShowErrorMessage(errorMessage); this.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, (Action)(() => { if (this.ErrorCallback != null) { this.ErrorCallback(this); this.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, (Action)(() => { RegainFocus(); EnableKeyboardLostFocus(); })); } else { RegainFocus(); EnableKeyboardLostFocus(); } })); } void OnBoxMouseUp(object sender, MouseButtonEventArgs e) { // disable the context menu for textbox and combobox if (e.ChangedButton == MouseButton.Right && e.RightButton == MouseButtonState.Released) { e.Handled = true; } } #region ICaseKeyBoxView Implementation public bool DisplayHintText { get { return (bool)GetValue(DisplayHintTextProperty); } set { SetValue(DisplayHintTextProperty, value); } } public object Value { get { return (object)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } } public Type ValueType { get { return (Type)GetValue(ValueTypeProperty); } set { SetValue(ValueTypeProperty, value); } } public void RegainFocus() { if (this.visibleBox != null) { Keyboard.Focus((IInputElement)this.visibleBox); } } #endregion #region Delegating Event Handlers public CaseKeyBoxViewModel ViewModel { get; set; } static void OnValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs eventArgs) { ((CaseKeyBox)sender).ViewModel.OnValueChanged(); } static void OnValueTypeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs eventArgs) { ((CaseKeyBox)sender).ViewModel.OnValueTypeChanged(); } void OnLabelGotFocus(object sender, RoutedEventArgs e) { this.ViewModel.OnLabelGotFocus(); e.Handled = true; } void OnLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) { e.Handled = true; if (ComboBoxHelper.ShouldFilterUnnecessaryComboBoxEvent(sender as ComboBox)) { return; } CommitChanges(); } public bool CommitChanges() { UpdateSource(this.visibleBox); if (this.CommitExplicitly || this.ViewModel.TextHasBeenChanged()) { string reason = null; if (!this.ViewModel.CanResolveInputText(out reason)) { ReportError(reason); return false; } else { return this.ViewModel.OnLostFocus(); } } else { CancelChanges(); return false; } } public void CancelChanges() { DisableKeyboardLostFocus(); this.ViewModel.OnEscapePressed(); // simulate cancel } void OnBoxLoaded(object sender, RoutedEventArgs e) { UIElement box = (UIElement)sender; ComboBox comboBox = box as ComboBox; if (comboBox != null && comboBox.IsVisible) { ComboBoxHelper.SynchronizeComboBoxSelection(comboBox, this.ViewModel.Text); } if (box.IsVisible) { box.Focus(); } Control control = sender as Control; if (control != null && control.Visibility == Visibility.Visible) { this.visibleBox = control; EnableKeyboardLostFocus(); } this.ViewModel.SaveOldText(); } void OnBoxUnloaded(object sender, RoutedEventArgs e) { if (this.visibleBox != null) { DisableKeyboardLostFocus(); this.visibleBox = null; } } void OnBoxKeyDown(object sender, KeyEventArgs e) { if (!CommitExplicitly) { if (e.Key == Key.Escape) { e.Handled = true; CancelChanges(); } else if (e.Key == Key.Enter) { e.Handled = true; CommitChanges(); } } } void UpdateSource(object sender) { if (sender is TextBox) { BindingExpression binding = ((TextBox)sender).GetBindingExpression(TextBox.TextProperty); if (binding != null) { binding.UpdateSource(); } } else if (sender is ComboBox) { BindingExpression binding = ((ComboBox)sender).GetBindingExpression(ComboBox.TextProperty); if (binding != null) { binding.UpdateSource(); } } } #endregion public void ResetText() { this.ViewModel.ResetText(); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Automation.cs
- DecimalStorage.cs
- SecurityPolicySection.cs
- LayoutUtils.cs
- EditorZone.cs
- DataGridViewCellLinkedList.cs
- FileClassifier.cs
- SHA512.cs
- TypedElement.cs
- SurrogateSelector.cs
- Font.cs
- RoutedEventConverter.cs
- DefaultHttpHandler.cs
- AffineTransform3D.cs
- DataSourceHelper.cs
- XmlFormatWriterGenerator.cs
- EntityException.cs
- AnnouncementSendsAsyncResult.cs
- UTF7Encoding.cs
- HtmlHead.cs
- SiteOfOriginPart.cs
- EventHandlingScope.cs
- DragSelectionMessageFilter.cs
- PropertyInformationCollection.cs
- ValueType.cs
- XPathNode.cs
- TextDpi.cs
- Pen.cs
- DbParameterCollection.cs
- FormClosedEvent.cs
- UnmanagedMarshal.cs
- TextDecorations.cs
- CodeCastExpression.cs
- ScriptControlDescriptor.cs
- XmlConvert.cs
- AttachedPropertyBrowsableForTypeAttribute.cs
- ListView.cs
- PageParser.cs
- ActivityDesignerResources.cs
- DataGridColumnDropSeparator.cs
- PrinterUnitConvert.cs
- CreateParams.cs
- SimpleWebHandlerParser.cs
- safex509handles.cs
- VirtualizedItemPattern.cs
- GlyphsSerializer.cs
- ToolStripGripRenderEventArgs.cs
- SourceCollection.cs
- AppDomainFactory.cs
- PackagePart.cs
- BorderGapMaskConverter.cs
- Renderer.cs
- SqlCacheDependency.cs
- CalendarDay.cs
- HatchBrush.cs
- PropertyInfo.cs
- InputMethod.cs
- ActiveXSite.cs
- SqlConnectionString.cs
- ApplicationId.cs
- TypeContext.cs
- _Win32.cs
- InputReport.cs
- StateDesigner.TransitionInfo.cs
- BamlRecordWriter.cs
- UnsafeNativeMethods.cs
- MiniModule.cs
- DesignerProperties.cs
- MenuItemStyleCollection.cs
- SoapTypeAttribute.cs
- DesignerActionTextItem.cs
- TypeUtil.cs
- TextOptionsInternal.cs
- Parser.cs
- DataSourceProvider.cs
- FrameworkElementFactoryMarkupObject.cs
- Privilege.cs
- ScrollChrome.cs
- CompiledQueryCacheKey.cs
- PropertyCollection.cs
- TableColumnCollectionInternal.cs
- UnionExpr.cs
- OleDbTransaction.cs
- EdmFunction.cs
- ChineseLunisolarCalendar.cs
- InputQueue.cs
- WorkflowViewService.cs
- CustomError.cs
- CompModSwitches.cs
- AudioFileOut.cs
- DataGridSortCommandEventArgs.cs
- Timer.cs
- recordstatefactory.cs
- Camera.cs
- InternalsVisibleToAttribute.cs
- XmlSchemaSimpleTypeUnion.cs
- SafeNativeMethodsOther.cs
- ForwardPositionQuery.cs
- SharedUtils.cs
- DropTarget.cs