0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【WPF】TextBoxの自動改行時の行数を取得する方法

Last updated at Posted at 2025-05-10

いいね、悪いね、コメントでの Ass fuckなどが励みになります

WPFのテキストボックスが自動改行("Wrap")のとき、そのテキストの行数を取得できるのか実験してみました。

自動改行でも実際は.netのメソッドで簡単にできるっぽいですね

以前の.netじゃ不可だったとか?知らんけど

Microsoft Learn

指定された文字インデックスが含まれている行に対応する、0 から始まる行インデックスを返します。 

とある

対応

Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

XAML

外観
<Window x:Class="WpfTextBox_BrakeLineCounter.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfTextBox_BrakeLineCounter"
        mc:Ignorable="d"
        Title="MainWindow" Height="160" Width="200">
    <Grid >
        <!--自動改行を設定-->
        <TextBox x:Name="MyTextBox" 
                 TextWrapping="Wrap"
                 AcceptsReturn="True"
                 Width="200" 
                 Height="100" 
                 TextChanged="MyTextBox_TextChanged"
                 />
        <TextBlock x:Name="LineCountTextBlock" 
                   Margin="0,0,0,0" 
                   Text="Line Count: 0"
                   VerticalAlignment="Top"/>        
    </Grid>
</Window>

メソッド定義

  
        public static int GetWrappedLineCount(TextBox textBox)
        {
            if (!textBox.IsLoaded)
            {
                // TextBoxがまだ描画されていない場合、正しいLineCountが得られない
                return -1;
            }
            int lastCharIndex = textBox.Text.Length;

         
            ///文字列のインデックスを取得
            int lineIndex = textBox.GetLineIndexFromCharacterIndex(lastCharIndex);
            return lineIndex + 1; // 0-based indexなので+1
        }
 
        private void MyTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
          ///非同期実行にしておく  
           MyTextBox.Dispatcher.InvokeAsync(() =>
            {
                int lineCount = TextBoxHelper.GetWrappedLineCount(MyTextBox);

                LineCountTextBlock.Text = $"Line Count: {lineCount}";

            });

        }

実行(自動改行"wrap"でも実は関係ない)

布教活動も兼ねて

あとがき

生成AIによると不可能みたいな事言ってたんだけど、FormatTextを生成するやり方だったんで。
生成AIに頼りすぎると却って正解が遠のきます

Stack OverFlowも出鱈目が多くてそんなにあてにならない:sweat_smile:

↓笑えるほど役に立たなかった記事
Detect the number of line breaks in TextBlock with wrap?
https://stackoverflow.com/questions/15950210/detect-the-number-of-line-breaks-in-textblock-with-wrap

....ぶっちゃけ真面目に勉強しようかなと思うこともたまにあります。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?