コード例 #1
0
        private void FindOrReplace(FindControlMode mode)
        {
            ctlFind.Mode       = mode;
            ctlFind.Visibility = Visibility.Visible;

            /* About focusing the ctlFind.txtFind:
             *
             * Calling the FocusFindTB should be enough. And it is, except for the first time the find bar is shown. Then for some reason the focus stays in the text editor control.
             * When debugging I found that this works even the first time, after the focus returns from the debugger to the app window. So, I simulated this with invoking the invalidate visual.
             * To see this behavior comment out the if block.
             *
             *  Usually, I would handle this differently with:
             *   var win = Window.GetWindow(this);
             *   FocusManager.SetFocusedElement(win, ctlFind.txtFind);
             *
             * This really should be enough for focus to work in a WPF app, but I get null for win here. I think it's related to this control being hosted in a WinForms app.
             * I've also tried going down the call stack and calling focus on the different controls this control is hosted in, but it had no effect.
             * */

            if (!_findShownAlready)
            {
                _shouldShowFind = true;
                this.InvalidateVisual();
                return;
            }

            FocusFindTB();
        }
コード例 #2
0
        private void FindOrReplace(FindControlMode mode)
        {
            ctlFind.Mode = mode;
            ctlFind.Visibility = Visibility.Visible;

            /* About focusing the ctlFind.txtFind:
             * 
             * Calling the FocusFindTB should be enough. And it is, except for the first time the find bar is shown. Then for some reason the focus stays in the text editor control.
             * When debugging I found that this works even the first time, after the focus returns from the debugger to the app window. So, I simulated this with invoking the invalidate visual.
             * To see this behavior comment out the if block.
             *
             *  Usually, I would handle this differently with:
             *   var win = Window.GetWindow(this);
             *   FocusManager.SetFocusedElement(win, ctlFind.txtFind);
             * 
             * This really should be enough for focus to work in a WPF app, but I get null for win here. I think it's related to this control being hosted in a WinForms app.
             * I've also tried going down the call stack and calling focus on the different controls this control is hosted in, but it had no effect.
             * */

            if (!_findShownAlready)
            {
                _shouldShowFind = true;
                this.InvalidateVisual();
                return;
            }

            FocusFindTB();
        }