Problem setting cursor in editor programmatically

SyntaxEditor for WPF Forum

Posted 8 years ago by Tino Schnerwitzki
Version: 11.1.0543
Avatar
Hi,

I´m using a SyntaxEditor inside a DockSite (or better its DocumentWindows). What I want to do is that as soon as I activate a DocumentWindow the cursor should be placed inside its SyntaxEditor.

The problem is that neither setting the Postion of the caret nor setting the selection and calling .Focus() on the Editor seem to work.

Any ideas what might be wrong?

Here is a simple sample:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:docking="http://schemas.actiprosoftware.com/winfx/xaml/docking"
        xmlns:syntaxEditor="http://schemas.actiprosoftware.com/winfx/xaml/syntaxeditor"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <docking:DockSite WindowActivated="DockSite_WindowActivated">
            <docking:SplitContainer>
                <docking:Workspace>
                    <docking:TabbedMdiHost x:Name="tabbedMdiHost">
                        <docking:TabbedMdiContainer>
                            <docking:DockingWindow>
                                <syntaxEditor:SyntaxEditor x:Name="e1" />
                            </docking:DockingWindow>
                            <docking:DockingWindow>
                                <syntaxEditor:SyntaxEditor x:Name="e2" />
                            </docking:DockingWindow>
                        </docking:TabbedMdiContainer>
                    </docking:TabbedMdiHost>
                </docking:Workspace>
            </docking:SplitContainer>
        </docking:DockSite>
    </Grid>
</Window>

And in codebehind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ActiproSoftware.Windows.Controls.Docking;
using ActiproSoftware.Windows.Controls.SyntaxEditor;
using ActiproSoftware.Text;

namespace WpfApplication2
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void DockSite_WindowActivated(object sender, ActiproSoftware.Windows.Controls.Docking.DockingWindowEventArgs e)
        {
            var editor = tabbedMdiHost.FindChild<SyntaxEditor>();
            editor.ActiveView.Selection.StartPosition = new TextPosition(0, 0);
            editor.Focus();
        }
    }

    public static class Test
    {
        public static T FindChild<T>(this DependencyObject current) where T : DependencyObject
        {
            do
            {
                if (current is T)
                {
                    return (T)current;
                }
                current = VisualTreeHelper.GetChild(current, 0);
            }
            while (current != null);
            return null;
        }
    }
}

Comments (1)

Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Tino,

You should not use the DockingWindow type directly like that. It is intended to only be used as a base class. You should use either a DocumentWindow or a ToolWindow in it's place.

Once you change that, it should properly move focus.


Actipro Software Support

The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.