当前位置: 首页>>代码示例>>C#>>正文


C# UIElement.Focus方法代码示例

本文整理汇总了C#中UIElement.Focus方法的典型用法代码示例。如果您正苦于以下问题:C# UIElement.Focus方法的具体用法?C# UIElement.Focus怎么用?C# UIElement.Focus使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在UIElement的用法示例。


在下文中一共展示了UIElement.Focus方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SetFocusedElement

 /// <summary>
 /// Sets the focused <see cref="UIElement"/> within the specified focus scope
 /// </summary>
 /// <param name="focusScope">The <see cref="IUIElement"/> that represents the scope for which to set the focused element</param>
 /// <param name="focusedElement">The <see cref="UIElement"/> to set the focus to</param>
 public static void SetFocusedElement(IUIElement focusScope, UIElement focusedElement)
 {
     UIElement toUnfocus;
     if (!focusScope.DependencyProperties.ContainsKey(FocusManager.FocusedElementProperty))
     {
        FocusManager.AppendFocusProperties(focusScope);
     }
     toUnfocus = focusScope.GetValue<UIElement>(FocusManager.FocusedElementProperty);
     if(toUnfocus != null)
     {
         toUnfocus.Unfocus();
     }
     focusScope.SetValue(FocusManager.FocusedElementProperty, focusedElement);
     focusedElement.Focus();
 }
开发者ID:yonglehou,项目名称:Photon,代码行数:20,代码来源:FocusManager.cs

示例2: AsyncSetFocusAndCapture

        public static void AsyncSetFocusAndCapture(UIElement element,
            Func<bool> getter,
            UIElement targetCapture,
            UIElement targetFocus)
        {
            element.Dispatcher.BeginInvoke(
                (Action)delegate()
                {
                    if (getter())
                    {
                        if (targetFocus != null && !targetFocus.IsKeyboardFocusWithin)
                        {
                            targetFocus.Focus();
                        }

                        if (targetCapture != null &&
                            (Mouse.Captured == null ||
                            !IsCaptureInSubtree(targetCapture)))
                        {
                            Mouse.Capture(targetCapture, CaptureMode.SubTree);
                        }
                    }
                },
                DispatcherPriority.Input);
        }
开发者ID:kasicass,项目名称:kasicass,代码行数:25,代码来源:RibbonHelper.cs

示例3: ReacquireCapture

 private static void ReacquireCapture(UIElement targetCapture, UIElement targetFocus)
 {
     Mouse.Capture(targetCapture, CaptureMode.SubTree);
     if (targetFocus != null && !targetFocus.IsKeyboardFocusWithin)
     {
         targetFocus.Focus();
     }
 }
开发者ID:kasicass,项目名称:kasicass,代码行数:8,代码来源:RibbonHelper.cs

示例4: HandleDropDownKeyDown

        internal static void HandleDropDownKeyDown(
            object sender, KeyEventArgs e, Func<bool> gettor, Action<bool> settor, UIElement targetFocusOnFalse, UIElement targetFocusContainerOnTrue)
        {
            Key key = e.Key;
            switch (key)
            {
                case Key.Escape:
                    {
                        if (gettor())
                        {
                            settor(false);
                            e.Handled = true;
                            if (targetFocusOnFalse != null)
                            {
                                targetFocusOnFalse.Focus();
                            }
                        }
                    }
                    break;

                case Key.System:
                    if ((e.SystemKey == Key.LeftAlt) ||
                        (e.SystemKey == Key.RightAlt) ||
                        (e.SystemKey == Key.F10))
                    {
                        if (gettor())
                        {
                            // Raise DismissPopup event and hence the key down event.
                            UIElement uie = sender as UIElement;
                            if (uie != null)
                            {
                                RibbonDismissPopupEventArgs dismissArgs = new RibbonDismissPopupEventArgs();
                                uie.RaiseEvent(dismissArgs);
                                e.Handled = true;
                            }
                        }
                    }
                    break;
                case Key.F4:
                    {
                        if (gettor())
                        {
                            settor(false);
                            e.Handled = true;
                            if (targetFocusOnFalse != null)
                            {
                                targetFocusOnFalse.Focus();
                            }
                        }
                        else
                        {
                            settor(true);
                            if (targetFocusContainerOnTrue != null)
                            {
                                targetFocusContainerOnTrue.Dispatcher.BeginInvoke(
                                    (Action)delegate()
                                    {
                                        targetFocusContainerOnTrue.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
                                    },
                                    DispatcherPriority.Input,
                                    null);
                            }
                            e.Handled = true;
                        }

                        // Technically one needs to change active key tip scope,
                        // but since we do not have public api to do that yet,
                        // we dismiss keytips
                        KeyTipService.DismissKeyTips();
                    }
                    break;
            }
        }
开发者ID:kasicass,项目名称:kasicass,代码行数:73,代码来源:RibbonHelper.cs

示例5: ForceFocus

    internal bool ForceFocus( UIElement element )
    {
      if( element == null )
        return false;

      Debug.Assert( !this.InhibitPreviewGotKeyboardFocus );
      this.InhibitPreviewGotKeyboardFocus = true;

      try
      {
        return element.Focus();
      }
      finally
      {
        this.InhibitPreviewGotKeyboardFocus = false;
      }
    }
开发者ID:wangws556,项目名称:duoduo-chat,代码行数:17,代码来源:DataGridControl.cs

示例6: ForceFocus

    internal bool ForceFocus( UIElement element )
    {
      if( element == null )
        return false;

      using( m_inhibitPreviewGotKeyboardFocus.Set() )
      {
        return element.Focus();
      }
    }
开发者ID:austinedeveloper,项目名称:WpfExtendedToolkit,代码行数:10,代码来源:DataGridControl.cs

示例7: SetFocusUIElementHelper

    internal static bool SetFocusUIElementHelper( UIElement toFocus )
    {
      if( toFocus == null )
        return false;

      try
      {
        if( toFocus.Focus() )
          return true;
      }
      catch
      {
      }

      if( toFocus.IsKeyboardFocusWithin )
        return true;

      toFocus = DataGridControl.FindFirstFocusableChild( toFocus );

      if( toFocus == null )
        return false;

      try
      {
        return toFocus.Focus();
      }
      catch
      {
        return false;
      }
    }
开发者ID:wangws556,项目名称:duoduo-chat,代码行数:31,代码来源:DataGridControl.cs

示例8: Fade

        private void Fade(UIElement FadeToControl)
        {
            CurrentControl.IsEnabled = false;
            // Create a storyboard to contain the animations.
            var storyboard = new Storyboard();
            var storyboard2 = new Storyboard();

            // Create a DoubleAnimation to fade the not selected option control
            var animationIn = new DoubleAnimation();
            var animationOut = new DoubleAnimation();

            animationIn.From = 1.0;
            animationIn.To = 0.0;
            animationOut.From = 0.0;
            animationOut.To = 1.0;
            animationIn.Duration =  new Duration(TimeSpan.FromMilliseconds(500));
            animationOut.Duration = new Duration(TimeSpan.FromMilliseconds(500));

            animationIn.Completed += (x, y) =>
                {
                    CurrentControl.IsEnabled = true;
                    MainGrid.Children.Remove(CurrentControl);
                    MainGrid.Children.Add(FadeToControl);
                    //CurrentControl.Visibility = Visibility.Hidden;
                    FadeToControl.Visibility = Visibility.Visible;
                    storyboard2.Begin(this);
                };
            // Configure the animation to target de property Opacity
            Storyboard.SetTarget(animationIn, CurrentControl);
            Storyboard.SetTargetProperty(animationIn, new PropertyPath(Control.OpacityProperty));
            Storyboard.SetTarget(animationOut, FadeToControl);
            Storyboard.SetTargetProperty(animationOut, new PropertyPath(Control.OpacityProperty));

            // Add the animation to the storyboard
            storyboard.Children.Add(animationIn);
            storyboard2.Children.Add(animationOut);

            // Begin the storyboard
            storyboard.Begin(this);
            CurrentControl = FadeToControl;

            FadeToControl.Focus();
            /*
            if (FadeToControl is TekstComponent)
            {
                var tc = FadeToControl as TekstComponent;
                tc.InputBox.Focusable = true;
                FocusManager.SetFocusedElement(this, tc.InputBox);
                Keyboard.Focus(InputBox);
            }*/
        }
开发者ID:kypp,项目名称:word-recall-test,代码行数:51,代码来源:MainWindow.xaml.cs

示例9: SetFocusIfNoMenuIsOpened

 public static void SetFocusIfNoMenuIsOpened(UIElement elem)
 {
     if (!Instance.IsAnyMenuOpened())
         elem.Focus();
 }
开发者ID:DeepSkyFire,项目名称:dnSpy,代码行数:5,代码来源:MainWindow.xaml.cs

示例10: SetFocus

        void SetFocus(UIElement element)
        {
            element.Focus();

            // when focusing on textbox, set cursor to the end
            TextBox textBox = element as TextBox;
            if (null != textBox || !string.IsNullOrEmpty(textBox.Text))
            {
                textBox.SelectionStart = textBox.Text.Length;
            }
        }
开发者ID:backlune,项目名称:SPLE,代码行数:11,代码来源:Window1.xaml.cs

示例11: GetAutoCompleteUIElement

        /// <summary>Returns an existing reference to the UI used for the drop-down (or creates a new one)</summary>
        /// <param name="attachedTo">Object the UI is to be attached to</param>
        /// <returns>UI element</returns>
        private static AutoComplete GetAutoCompleteUIElement(UIElement attachedTo)
        {
            var dropDownUI = GetAutoCompleteUI(attachedTo);
            if (dropDownUI == null)
            {
                dropDownUI = new AutoComplete();

                attachedTo.GotFocus += (sender, e) =>
                    {
                        var adornerLayer = AdornerLayer.GetAdornerLayer(attachedTo);
                        if (adornerLayer == null) return;
                        var currentAdorners = adornerLayer.GetAdorners(attachedTo);
                        var adornerFound = false;
                        if (currentAdorners != null)
                            if (currentAdorners.OfType<AutoCompleteDropDownAdorner>().Any())
                                adornerFound = true;

                        if (dropDownUI._adornerLoaded && adornerFound) return;
                        var parent = VisualTreeHelper.GetParent(dropDownUI);
                        if (parent != null)
                        {
                            var oldAdorner = parent as AutoCompleteDropDownAdorner;
                            if (oldAdorner != null)
                                oldAdorner.DisconnectVisualChild(dropDownUI);
                        }
                        var adorner = new AutoCompleteDropDownAdorner(attachedTo, dropDownUI);
                        var localAdorner = adorner;
                        adornerLayer.Add(adorner);
                        dropDownUI._adornerLoaded = true;

                        if (dropDownUI.Items.Count > 0) dropDownUI.Visibility = Visibility.Visible;
                        localAdorner.InvalidateMeasure();
                        localAdorner.InvalidateArrange();
                    };
                attachedTo.PreviewKeyDown += (sender, e) =>
                    {
                        switch (e.Key)
                        {
                            case Key.Up:
                            case Key.Down:
                                {
                                    var index = dropDownUI.SelectedIndex;
                                    switch (e.Key)
                                    {
                                        case Key.Down:
                                            index++;
                                            if (index >= dropDownUI.Items.Count)
                                                index = dropDownUI.Items.Count - 1;
                                            break;
                                        case Key.Up:
                                            index--;
                                            if (index < 0)
                                                index = 0;
                                            break;
                                    }
                                    dropDownUI.SelectedIndex = index;
                                    dropDownUI.ScrollIntoView(dropDownUI.SelectedItem);
                                    e.Handled = true;
                                }
                                break;
                            case Key.Enter:
                                {
                                    if (dropDownUI.SelectedIndex > -1)
                                        SetAutoCompleteSelectedItem(attachedTo, dropDownUI.SelectedItem);
                                    dropDownUI.Visibility = Visibility.Collapsed;
                                    dropDownUI._uiEligibleToBeVisible = false;
                                    attachedTo.Focus();
                                    var text = attachedTo as TextBox;
                                    if (text != null)
                                        text.SelectionStart = text.Text.Length;
                                }
                                break;
                        }
                    };
                attachedTo.KeyUp += (sender, e) =>
                    {
                        if (e.Key != Key.Enter && dropDownUI.Visibility == Visibility.Collapsed)
                            if (dropDownUI.Items.Count > 0)
                                dropDownUI.Visibility = Visibility.Visible;
                            else
                                dropDownUI._uiEligibleToBeVisible = true;
                        if (dropDownUI.Visibility == Visibility.Visible && dropDownUI.Items.Count == 0) dropDownUI.Visibility = Visibility.Collapsed;
                    };
                dropDownUI.PreviewKeyDown += (sender, e) =>
                    {
                        if (e.Key != Key.Enter) return;
                        if (dropDownUI.SelectedIndex > -1)
                            SetAutoCompleteSelectedItem(attachedTo, dropDownUI.SelectedItem);
                        dropDownUI.Visibility = Visibility.Collapsed;
                        dropDownUI._uiEligibleToBeVisible = false;
                    };
                dropDownUI.MouseLeftButtonUp += (sender, e) =>
                    {
                        if (dropDownUI.SelectedIndex <= -1) return;
                        SetAutoCompleteSelectedItem(attachedTo, dropDownUI.SelectedItem);
                        dropDownUI.Visibility = Visibility.Collapsed;
                        dropDownUI._uiEligibleToBeVisible = false;
//.........这里部分代码省略.........
开发者ID:sat1582,项目名称:CODEFramework,代码行数:101,代码来源:AutoComplete.cs

示例12: TextSuggestionsControl

        public TextSuggestionsControl(TextBox Input, int MaxResults, UIElement Unfocus, Canvas Container)
        {
            this.Enabled = true;
            this.Input = Input;
            this.MaxResults = MaxResults;
            this.Unfocus = Unfocus;

            var Results = new List<TextBox>();
            var ResultsLayers = new List<Rectangle>();
            var DataSelectedLastTimeDefault = new Match[0];
            var DataSelectedLastTime = DataSelectedLastTimeDefault;

            Action ClearResults =
                delegate
                {
                    Results.AsEnumerable().Orphanize();
                    Results.Clear();

                    ResultsLayers.AsEnumerable().Orphanize();
                    ResultsLayers.Clear();

                    DataSelectedLastTime = DataSelectedLastTimeDefault;
                };

            var FriendlyFocusChange = false;

            Action<UIElement> SetFriendlyFocus =
                e =>
                {
                    FriendlyFocusChange = true;
                    e.Focus();
                    FriendlyFocusChange = false;
                };

            Action CancelExitDefault = delegate { };
            Action CancelExit = CancelExitDefault;

            Action<Rectangle, TextBox> DefaultActivate =
                (r_Below, r) =>
                {
                    if (!DisableColorChange)
                    {
                        r_Below.Fill = this.ActiveResultBackground;
                        r.Foreground = this.ActiveResultForeground;
                    }

                    if (this.Activate != null)
                        this.Activate(r_Below, r);
                };

            Action<Rectangle, TextBox> DefaultDeactivate =
                (r_Below, r) =>
                {
                    if (!DisableColorChange)
                    {
                        r_Below.Fill = this.InactiveResultBackground;
                        r.Foreground = this.InactiveResultForeground;
                    }

                    if (this.Deactivate != null)
                        this.Deactivate(r_Below, r);
                };

            Action CancelUpdateDelayDefault = delegate { };
            Action CancelUpdateDelay = CancelUpdateDelayDefault;

            Action StartExit =
                delegate
                {
                    if (!FriendlyFocusChange)
                    {
                        CancelExit = 100.AtDelay(
                            delegate
                            {
                                CancelExit = CancelExitDefault;

                                CancelUpdateDelayDefault();
                                ClearResults();

                                if (this.Exit != null)
                                    this.Exit();
                            }
                        ).Stop;

                    }
                };


            Action<TextBox> ApplyFocusKeys =
                e =>
                {
                    e.KeyUp +=
                        (sender, ev) =>
                        {
                            if (!this.Enabled)
                                return;

                            if (ev.Key == Key.Escape)
                            {
                                Unfocus.Focus();
//.........这里部分代码省略.........
开发者ID:exaphaser,项目名称:JSC-Cross-Compiler,代码行数:101,代码来源:TextSuggestionsControl.cs

示例13: OpenMenu

        internal static ContextMenu OpenMenu(UIElement target, out Clean cleaner)
        {
            cleaner = null;

            FrameworkElement owner = null;
            var tree = TreeUtilityInTarget.VisualTree(target, TreeRunDirection.Ancestors);
            foreach (var e in tree)
            {
                var f = e as FrameworkElement;
                if (f != null && f.ContextMenu != null)
                {
                    owner = f;
                    break;
                }
            }
            if (owner == null)
            {
                throw new NotSupportedException();
            }
            var menu = owner.ContextMenu;
            int count = menu.CommandBindings.Count;

            foreach (var e in TreeUtilityInTarget.VisualTree(target, TreeRunDirection.Ancestors))
            {
                var u = e as UIElement;
                if (u != null && u.CommandBindings != null)
                {
                    foreach (CommandBinding command in u.CommandBindings)
                    {
                        menu.CommandBindings.Add(command);
                    }
                }
            }
            target.Focus();
            menu.IsOpen = true;
            InvokeUtility.DoEvents();

            //数を元に戻す
            cleaner = () =>
            {
                while (count < menu.CommandBindings.Count)
                {
                    menu.CommandBindings.RemoveAt(menu.CommandBindings.Count - 1);
                }
                menu.IsOpen = false;
            };

            return menu;
        }
开发者ID:Roommetro,项目名称:Friendly.WPFStandardControls,代码行数:49,代码来源:WPFContextMenu.cs

示例14: MoveFocusIfPossible

 private static void MoveFocusIfPossible(UIElement element, int delay)
 {
     if (element == null) return;
     if (element.IsVisible)
         element.Focus();
     else
         // Not there yet... we can try again
         FocusDelayed(element, delay: delay);
 }
开发者ID:sat1582,项目名称:CODEFramework,代码行数:9,代码来源:FocusHelper.cs


注:本文中的UIElement.Focus方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。