#932 – Making a TextBox Read-Only or Disabled

You can control a couple of different things with the IsEnabled and IsReadOnly properties of a TextBox.

  • IsEnabled – When false, user can’t interact with the control in any way and the control is greyed out.  (Default is true)
  • IsReadOnly – When true, user can’t edit or enter text, but can still scroll, select text and copy.

Typical combinations:

  • IsEnabledtrue, IsReadOnly = false — standard behavior, editable text
  • IsEnabled = true, IsReadOnly = true — read-only text, user can scroll/copy
  • IsEnabled = false — user can’t interact with TextBox at all
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <TextBox Name="txtMain" Margin="5"
                 Text="{Binding SomeText}"
                 TextWrapping="Wrap"
                 VerticalScrollBarVisibility="Auto"
                 IsReadOnly="{Binding ElementName=chkReadOnly, Path=IsChecked}"
                 IsEnabled="{Binding ElementName=chkEnabled, Path=IsChecked}"/>
        <StackPanel Orientation="Horizontal" Grid.Row="1">
            <CheckBox Name="chkReadOnly" Content="IsReadOnly" />
            <CheckBox Name="chkEnabled" Content="IsEnabled" />
        </StackPanel>
    </Grid>

932-001
932-002
932-003

Advertisement