Unable to make a popup that shows up in a RichTextBox when a character is placed

51 Views Asked by At

I want to make a suggestion box that shows up certain options when the character "@" is placed in the RichTextBox.

This is what I've tried.

Codebehind:

private void noteEditor_TextChanged(object sender, TextChangedEventArgs e)
{ 
    range.Select(NoteEditor.CaretPosition.GetPositionAtOffset(-1), NoteEditor.CaretPosition);
    if (range.Text == "@")
    {
        Rect rect = NoteEditor.CaretPosition.GetCharacterRect(LogicalDirection.Backward);
        Point point = NoteEditor.PointToScreen(rect.BottomRight); 

        SuggestBox.HorizontalOffset = point.X;
        SuggestBox.VerticalOffset = point.Y;
        SuggestBox.IsOpen = true;
    }
}

XAML

<Grid Margin="-1" Background="#101010">
    <Grid.RowDefinitions>
        <RowDefinition Height="30"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    
    <!--Title-->
    <TextBlock x:Name="NoteTitle" Grid.Row="0" FontSize="10" Background="#101010" Foreground="White" Margin="0,0,0,1"/>

    <!--Text Editor-->
        <RichTextBox x:Name="NoteEditor" Block.LineHeight="1" TextChanged="noteEditor_TextChanged" Grid.Row="1" Background="#101010" Foreground="White"/>
    <Popup x:Name="SuggestBox" IsOpen="False" Placement="Left" Grid.Row="1">
        <ListBox x:Name="SuggestionOptions" Background="White">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Name,Mode=OneWay}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Popup>
</Grid>
0

There are 0 best solutions below