dotnet / dotnet/wpf

[API Proposal]: Add GetCharacterIndexFromPoint method to TextBlock

Open
#9,235 3 comments 2 reactions 0 assignees View on GitHub
API suggestion
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Background and motivation

In my app I need to get the character index from the mouse position. Even though I don't need the capabilities of a `TextBox`, because `TextBlock` does not have a method like [GetCharacterIndexFromPoint](https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.textbox.getcharacterindexfrompoint), I need to use the TextBox control instead. The issue is my app creates _lots of_ TextBoxes (which could have been TextBlocks if a TextBlock supported the aforementioned method) and a TextBox is more resource hungry than a TextBlock. So I'd like to avoid paying the price of a TextBox unnecessarily.

### API Proposal

```csharp
public int GetCharacterIndexFromPoint (System.Windows.Point point, bool snapToText);
```

### API Usage

```csharp
TextBlock textBlock = new();
int characterPosition = textBlock.GetCharacterIndexFromPoint(Mouse.GetPosition(textBlock), false);
```

### Alternative Designs

_No response_

### Risks

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.