Adam
Adam

Reputation: 2061

C# - How to create multi-line read-only TextBox in WPF?

In C# WPF application, I want to create a TextBox control that is both multi-line and read-only with proper screen reader accessibility for text cursor navigation and narration. When I create the control like this:

<TextBox IsReadOnly="True" />

...and populate it with a multi-line string, then I am unable to navigate in the TextBox field using the Down or Up arrow keys by line. With screen reader, it behaves as a single-line field instead. I also need the possibility to programatically change the text cursor position, as I will be implementing find text feature within that TextBox.

I've also tried setting the TextWrapping="Wrap" and AcceptsReturn="True" properties, but with no success. Please, what I am missing here?

Upvotes: 0

Views: 135

Answers (3)

Adam
Adam

Reputation: 2061

Just setting:

<TextBox IsReadOnly="True" IsReadOnlyCaretVisible="True" />

on the TextBox control solved the problem.

Upvotes: 1

abbas akbarzade
abbas akbarzade

Reputation: 33

You can use this code written with Material Design library. Of course, you can get ideas from this code and use it in your program without using Material Design. The code is written as XAML follows:

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition
      Height="Auto" />
    <RowDefinition />
  </Grid.RowDefinitions>
  <CheckBox x:Name="RichTextBoxIsReadOnlyCheckBox"
 Content="Rich Text Box: IsReadOnly" IsChecked="True" />
  <RichTextBox Grid.Row="1" Height="80"
    MinWidth="280" materialDesign:HintAssist.Hint="Multiline text"
    AcceptsReturn="True" IsDocumentEnabled="True" 
IsReadOnly="{Binding IsChecked, ElementName=RichTextBoxIsReadOnlyCheckBox}" 
SpellCheck.IsEnabled="True" VerticalScrollBarVisibility="Auto">
    <FlowDocument>
      <Paragraph>
        <Run
          FontWeight="Bold">
        Rich Text Box.
      </Run>
        <LineBreak />
        <Run
          FontStyle="Italic">
        With formatting support
      </Run>
        <LineBreak />
        <Hyperlink
          Cursor="Hand"
          NavigateUri="...URI..."
          RequestNavigate="Hyperlink_OnRequestNavigate">
        Material Design in XAML
      </Hyperlink>
      </Paragraph>
    </FlowDocument>
  </RichTextBox>
</Grid>

This code allows you to have a multi-line text box and by using a check box you can achieve the read-only feature of the text box.

enter image description here

Also, in read-only mode, you can select the text inside the card as shown in the photo below

Upvotes: -1

DRapp
DRapp

Reputation: 48129

Looking for other properties...

MaxLines (number of allowed lines regardless of height). You could have a height of 120, but if max lines is 1, thats all you get.

Set MaxLines to like 10 or more as needed, then scroll bar visibility...

<TextBox x:Name="UxText2"
   Text="{Binding Text2}"
   Width="96"
   .. allow height to show multiple POSSIBLE rows
   Height="110"
   .. just an arbitrary number for max lines
   MaxLines="30" 
   .. scroll bar if more data than visibly allowed
   VerticalScrollBarVisibility="Visible"
   ... rest of your settings.
   >

Upvotes: -1

Related Questions