#932 – Making a TextBox Read-Only or Disabled
October 21, 2013 3 Comments
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:
- IsEnabled = true, 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>
Pingback: Dew Drop – October 21, 2013 (#1,649) | Morning Dew
You are doing excellent job but please include the solution/project files similar to CodeProject site. This will help people to download and run at their end. Again, appreciate for your hard work.
For this kind of tip, I prefer the xaml / cs fragments. Its a PITA to have to download yet another solution, when I usually have a sandbox project available.
This is a pretty cool and self-contained setup. I’m having some trouble getting the IsReadonly to respond on the UI from my VM. This simple approach will let me test it much easier.