Adin Barka

Adin Barka

  • 1.7k
  • 4
  • 392

Place caret be inside Richtextbox on app start

Dec 22 2023 11:07 AM

Hello everyone,

The question may sound simple enough, and I've been doing research for the last two days, with no avail. I am a pure hobbyist, and unfortunately the only one interested in programming amongst friends and relatives, so I do not have exactly a proper background based on personal interaction. Thus I may lack some terms used amongst programmers - but I will learn happyly any bit I can.

Starting point:

I have a wpf desktop app (.Net 6.0). All I am trying at the moment is to set up a basic text editor. My XAML at the moment looks something like this:

<UserControl x:Class="TextEditor.Views.TextEditorView"
 <!-- usual namespaces cut out for simplicity -->       
>
   
    <Grid Background="AliceBlue">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
 
        <Grid Grid.Row="0">
            <ToolBar>
                <ToggleButton x:Name="btnBold" Width="20" Height="20" Command="EditingCommands.ToggleBold">
                    <Image Source="/TextEditor;component/Images/bold.png"/>
                </ToggleButton>
                <ToggleButton x:Name="btnItalic" Width="20" Height="20" Command="EditingCommands.ToggleItalic">
                    <Image Source="/TextEditor;component/Images/italic.png"/>
                </ToggleButton>
                <ToggleButton x:Name="btnUnderline" Width="20" Height="20" Command="EditingCommands.ToggleUnderline">
                    <Image Source="/TextEditor;component/Images/underline.png"/>
                </ToggleButton>
                <Button x:Name="btnSave" Content="SAVE" Command="{Binding SaveCommand}" CommandParameter="{Binding ElementName=rtbContent, Path=Document}"/>
                <ComboBox Name="cmbFontFamilies" SelectionChanged="FontFamily_SelectionChanged" Height="23" Width="200">                   
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock VerticalAlignment="Center" Text="{Binding}" FontFamily="{Binding}" Height="20"/>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>
                <ComboBox Name="cmbFontSize" Height="23" Width="80" SelectionChanged="FontSize_SelectionChanged">
                    
                </ComboBox>
                <ComboBox Name="cmbFontColors" Height="23" Width="80" Focusable="False" >                   
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Rectangle Fill="{Binding Name}" Width="16" Height="16" Margin="0,2,5,2" />
                                <TextBlock Text="{Binding Name}" />
                            </StackPanel>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>
                
                
            </ToolBar>

        </Grid>

        <Grid Grid.Row="1">
            <RichTextBox x:Name="rtbEditor">

            </RichTextBox>
        </Grid>


    </Grid>
</UserControl>

 

So good so far. Now though I do use MVVM I leave things only concerning the view in codebehind (as you will see soon). Here is the code:

 public partial class TextEditorView : UserControl
 {
     public TextEditorView()
     {
         InitializeComponent();

         //cmbFontFamilies.ItemsSource = Fonts.SystemFontFamilies.OrderBy(f => f.Source);
         //cmbFontSize.ItemsSource = new List<double>() { 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72 };

         rtbEditor.Focus();
         

     }

     private void ApplyPropertyValueToSelectedText(DependencyProperty formattingProperty, object value)
     {
         if (value == null)
             return;

         rtbEditor.Focus();
        
         rtbEditor.Selection.ApplyPropertyValue(formattingProperty, value);
     }
     private void FontFamily_SelectionChanged(object sender, SelectionChangedEventArgs e)
     {
         //try
         //{
         //    FontFamily editValue = (FontFamily)e.AddedItems[0];
         //    ApplyPropertyValueToSelectedText(TextElement.FontFamilyProperty, editValue);
         //}
         //catch (Exception) { }
     }
     private void FontSize_SelectionChanged(object sender, SelectionChangedEventArgs e)
     {
         //try
         //{
         //    ApplyPropertyValueToSelectedText(TextElement.FontSizeProperty, e.AddedItems[0]);
         //}
         //catch (Exception) { }
     }

As you can see, I commented most of the code out, because it is not related to my question. Only the constructor is in effect now.

What I want to achieve:

Coming back to my question header: All I want to do at the moment, is to start up my app, and have the caret inside the richtextbox, so that the user can write in it right away. Turns out everything I tried so far failed. Some code I tried:

- rtbEditor.Focus();

- rtbEditor.SelectAll();

- rtbEditor.CaretPosition = rtbEditor.CaretPosition.Document.ContentStart;

- rtbEditor.Selection.Select = rtbEditor.CaretPosition.Document.ContentStart;

...and a lot of combinations thereof.

This should be an easy task, but obviously I am missing something crucial here. Any suggestions?

thank you very much, and best regards

Barka


Answers (1)