0
0

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 2024-12-08

僕だけ?

僕だけの仕打ちなのか分かりませんが、WPFのTextBox(1行)に範囲外に行くまで文字を打ち込んで矢印キーで追っかけていっても、末尾が見切れる(つまりカーソルが末尾に来ない)んですよね

とりあえずの解決策

何故か分かりませんが、HorizontalScrollBarプロパティをAutoがVisibleにすると問題なく動作するので、その状態でテンプレートの方をいじります

TextBoxに紐づけるStyle(仮にTextBoxStyle1とします)と、その内部のScrollViewerに紐づけるStyle(ScrollViewerStyle1とします)を作成
テンプレートの編集からコピーしてやってください

TextBoxStyle1の内部から以下のScrollViewerタグを探し出してScrollViewerStyle1に紐づけます

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True">
                   これ →    <ScrollViewer Style="{DynamicResource ScrollViewerStyle1}" x:Name="PART_ContentHost"  Focusable="false" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Hidden"/>
                        </Border>

次にScrollViewerStyle1のScrollBarにVisibility=Hiddenとします

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ScrollViewer}">
                        <Grid x:Name="Grid" Background="{TemplateBinding Background}">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="10*"/>
                                <RowDefinition Height="1*"/>
                            </Grid.RowDefinitions>
                            <Rectangle x:Name="Corner" Grid.Column="1" Fill="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" Grid.Row="1"/>
                            <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanHorizontallyScroll="False" ContentTemplate="{TemplateBinding ContentTemplate}" CanVerticallyScroll="False" Grid.Column="0" Content="{TemplateBinding Content}" CanContentScroll="{TemplateBinding CanContentScroll}" Margin="{TemplateBinding Padding}" Grid.Row="0"/>
                            <ScrollBar x:Name="PART_VerticalScrollBar" AutomationProperties.AutomationId="VerticalScrollBar" Cursor="Arrow" Grid.Column="1" Maximum="{TemplateBinding ScrollableHeight}" Minimum="0" Grid.Row="0" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource Mode=TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/>
               ここ →       <ScrollBar x:Name="PART_HorizontalScrollBar" Visibility="Hidden" AutomationProperties.AutomationId="HorizontalScrollBar" Cursor="Arrow" Grid.Column="0"  Minimum="0" Orientation="Horizontal" Grid.Row="1" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource Mode=TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"/>

あとは該当のTextBoxにTextBoxStyle1を紐づければ何とかなります

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?