2

I want to disable the selection of text in a textbox using a style preferably. The reason is that I have a style that makes a textbox look like a textblock until a certain criteria (IsRenaming) is met. These are the nodes of a treeview so I don't want the user to be able to select the text. Here is the style:

<Style x:Key="TextBlockStyleForTextBox" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="IsReadOnly" Value="True" />
</Style>

<Style x:Key="RenamingTextBox" TargetType="{x:Type TextBox}" BasedOn="{StaticResource TextBlockStyleForTextBox}">
        <Setter Property="Cursor" Value="Arrow"/>
        <Setter Property="Padding" Value="0" />
        <Setter Property="Margin" Value="0" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsRenaming}" Value="true">
                <DataTrigger.Setters>
                    <Setter Property="TextBox.IsReadOnly" Value="False" />
                    <Setter Property="Cursor" Value="IBeam" />
                    <Setter Property="Background" >
                        <Setter.Value>
                            <SolidColorBrush Color="{DynamicResource WhiteColor}"/>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="BorderThickness" Value="1" />
                    <Setter Property="Padding" Value="2" />
                    <Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}"/>
                    <Setter Property="behaviors:TextBoxBehavior.SelectAll" Value="True"/>
                </DataTrigger.Setters>
            </DataTrigger>
        </Style.Triggers>
    </Style>

I don't think I am overriding the IsReadOnly anywhere. Here is my textbox definition:

<DataTemplate x:Key="MyTemplate" >
        <TextBox Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" 
                 Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType=TreeView}}"
                 Style="{StaticResource RenamingTextBox}">

... etc

3
  • 1
    so what's not working? looks binding should trigger IsReadOnly just fine Commented Apr 17, 2012 at 20:22
  • What does your TextBox definition look like? Are you overriding the IsReadOnly property (or any others from your style) there? Commented Apr 17, 2012 at 20:24
  • @WonkotheSane - I added my definition of the TextBox above. They are defined in HierarchicalDataTemplates and I don't see where I override that property anywhere. Commented Apr 17, 2012 at 20:40

3 Answers 3

1

This is quick and dirty, but it's a good start/example from our conversation in the other answer, you can and additional tweaks to the style, but it totally works (just tested it)

<Style x:Key="RenamingTextBox" TargetType="{x:Type TextBox}">
        <Style.Setters>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Grid>
                            <TextBlock x:Name="block" Visibility="Visible" Text="{TemplateBinding Text}" Margin="1.5"/>
                            <TextBox x:Name="box" Visibility="Collapsed" Text="{TemplateBinding Text}"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <DataTrigger Binding="{Binding IsRenaming}" Value="true">
                                <DataTrigger.Setters>
                                    <Setter TargetName="block" Property="TextBox.Visibility" Value="Collapsed" />
                                    <Setter TargetName="box" Property="TextBox.Visibility" Value="Visible" />
                                </DataTrigger.Setters>
                            </DataTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style.Setters>
    </Style>    
Sign up to request clarification or add additional context in comments.

Comments

1

Well, one quick way to disallow text from being highlighted is to add to your styles toggle of IsEnabled. Otherwise, you might want to override a TextBox's Template, with a StackPanel, that toggles the TextBox and a TexBlock - super easy to do, and ton of samples all over, even here on stack overlow. Here's a your modified style with IsEnabled..

<Style x:Key="TextBlockStyleForTextBox" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="IsReadOnly" Value="True" />
        <Setter Property="IsEnabled" Value="False"/>
    </Style>

    <Style x:Key="RenamingTextBox" TargetType="{x:Type TextBox}" BasedOn="{StaticResource TextBlockStyleForTextBox}">
        <Setter Property="Cursor" Value="Arrow"/>
        <Setter Property="Padding" Value="0" />
        <Setter Property="Margin" Value="0" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsRenaming}" Value="true">
                <DataTrigger.Setters>
                    <Setter Property="TextBox.IsReadOnly" Value="False" />
                    <Setter Property="IsEnabled" Value="True"/>
                    <Setter Property="Cursor" Value="IBeam" />
                    <Setter Property="Background" >
                        <Setter.Value>
                            <SolidColorBrush Color="{DynamicResource WhiteColor}"/>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="BorderThickness" Value="1" />
                    <Setter Property="Padding" Value="2" />
                </DataTrigger.Setters>
            </DataTrigger>
        </Style.Triggers>
    </Style>

4 Comments

Only problem with setting IsEnabled is it then greys out the text and background making it LOOK disabled. I want it to look enabled...just not be selectable/highlightable :)
Example of toggling between textblock and textbox? Or can you add the link to an example for future reference so others don't need to search?
sure, give me a bit, I'll either find it, or quickly type that up for you
@KrisTrip, ok, just typed something up... seems faster than looking up , but I swear there's tone of stuff out there, I just don't have my google foo on today
0

Your styles are working just fine, as I just tested them! 3 possible problems:

  1. You are not binding the TextBox correctly
  2. Your are not assigning your style
  3. IsRemaining isn't raising property changed


    here's my test

    <Window x:Class="Sample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="500" Width="600" 
    x:Name="wnd">
       <Window.Resources>
           ...I put your styles in there..
       </Windows.Resources>
    <StackPanel>
        <TextBox DataContext="{Binding ViewModel, ElementName=wnd}" Style="{StaticResource RenamingTextBox}" >tata</TextBox>
        <Button Command="{Binding ViewModel.SomeCommand, ElementName=wnd}">change read only</Button>
    </StackPanel>
    

code behind (.xaml.cs)

public partial class MainWindow : Window
{
    public ViewModel ViewModel { get; set; }

    public MainWindow()
    {
        ViewModel = new ViewModel();
        InitializeComponent();          
    }
    }

public class ViewModel : INotifyPropertyChanged
{
    private DelegateCommand _someCmd;
    private bool _isRenaming;

    public DelegateCommand SomeCommand
    {
        get
        {
            return _someCmd ?? (_someCmd = new DelegateCommand(() =>
                                                                {
                                                                    IsRenaming = true;
                                                                }));
        }
    }

    public bool IsRenaming
    {
        get { return _isRenaming; }
        set
        {
            _isRenaming = value;
            RaisePropertyChanged("IsRenaming");
        }
    }
      }

3 Comments

The other parts of the style work great. When IsRenaming is true it makes it look like a textbox and allows editing. Normally it doesn't allow editing but still allows selection. I don't want users to be able to select the text (makes it hard to read).
well, i think you might want to look into toggling textBlock (for read only behavior) vs TextBox for editing.
you could set the system key color SystemColors.HighlightBrushKey (but then the rest of your app will default to that too): <Style.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightGreen"/> </Style.Resources>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.