Skip to main content

Microsoft Silverlight

Answered Question preventing greyed-out effect when TextBox.IsReadOnly = trueRSS Feed

(0)

peterdungan
peterdungan

Member

Member

223 points

150 Posts

preventing greyed-out effect when TextBox.IsReadOnly = true

I have textboxes which I want to be non-editable at certain times. However I don't want them to be greyed out when they are non-editable. Since converting the project to SL3 they are greyed out. Is this easy to change? If not I can just swap a TextBlock control with the same content for the contexts where they should not be editable.

Thanks

 

Sledge70
Sledge70

Contributor

Contributor

5892 points

1,042 Posts

Re: preventing greyed-out effect when TextBox.IsReadOnly = true

Easiest way would be to change the readonly style for the textbox.

Open your project in blend, right click on the textbox and select Edit Template -> Edit a Copy.

______________________________________________________
Please mark replies as answers if they answered your question...

Flexman on Silverlight

peterdungan
peterdungan

Member

Member

223 points

150 Posts

Re: preventing greyed-out effect when TextBox.IsReadOnly = true

Thanks.

The textboxes are added programmatically, not defined in xaml. I don't seem to be able to reference the template in the code. I have the template defined in App.Xaml. Haven't used control templates before so It's probably something basic...

 

Sledge70
Sledge70

Contributor

Contributor

5892 points

1,042 Posts

Re: Re: preventing greyed-out effect when TextBox.IsReadOnly = true

You can set the style of the textbox using the style property and by retreiving your new style from whatever resource you have stored it in.

Do a search on dynamic styles and you should get a few quick ideas on how to do this.

______________________________________________________
Please mark replies as answers if they answered your question...

Flexman on Silverlight

Sledge70
Sledge70

Contributor

Contributor

5892 points

1,042 Posts

Answered Question

Re: Re: preventing greyed-out effect when TextBox.IsReadOnly = true

There's a couple of things to do. Once you get started though it's quite easy to get the hang of it.

Just create a dummy project with a textbox on some control if you don't want to mess up your current app.

Create the template in Blend:
Right click TextBox -> Edit Template -> Edit a Copy
Give it a name MyTextBoxStyle
Click the New button to create a new resource file. Name it MyTextBoxStyleResource.xaml

Now open MyTextBoxStyleResource.xaml and search for this:
<Border x:Name="ReadOnlyVisualElement" Opacity="0" Background="#5EC9C9C9"/>

Change it to:
<Border x:Name="ReadOnlyVisualElement" Opacity="0" />

Move the new created resource file into your Silverlight project.

When you create your textbox in code set the style like this:
myNewTextBox.Style = Resources["MyTextBoxStyle"] as Style;

 

 

______________________________________________________
Please mark replies as answers if they answered your question...

Flexman on Silverlight

peterdungan
peterdungan

Member

Member

223 points

150 Posts

Re: Re: Re: preventing greyed-out effect when TextBox.IsReadOnly = true

Thanks for your help.

I followed your suggestion and also referenced the resource dictionary in the app.xaml file.

I was able to reference the style and achieve the desired effect with a textbox defined in the xaml.

However the style was not applied to the textboxes when I tried applying programmaticlly in the way you described, although no error was given.

I used breakpoints to see that the template property and the style property of the programmatically-added textboxes were null. The template property and the style property of the textbox defined in xaml both had values.

So I just need to find how to apply the style/template programmatically.

peterdungan
peterdungan

Member

Member

223 points

150 Posts

Re: Re: Re: Re: preventing greyed-out effect when TextBox.IsReadOnly = true

It works now.

I changed 

Resources["MyTextBoxStyle"] as Style;

to

Application.Current.Resources["MyTextBoxStyle"] as Style;

Thanks again for your help!

 edit:"Mark as answer" does not seem to be working for me on this atm.

Sledge70
Sledge70

Contributor

Contributor

5892 points

1,042 Posts

Re: Re: Re: Re: Re: preventing greyed-out effect when TextBox.IsReadOnly = true

Will take note of the correct way to resolve the resource.

Sometimes mark as answer doesn't work because the link you follow for example has a # symbol in it which makes the mark as answer fail. Removing the # and everything to the right of it and hitting enter will allow it to work.

______________________________________________________
Please mark replies as answers if they answered your question...

Flexman on Silverlight

iliya tretyakov
iliya tr...

Member

Member

38 points

33 Posts

Re: Re: Re: preventing greyed-out effect when TextBox.IsReadOnly = true

Let me some abuse style for TextBox only for text selection :)

in App.xaml

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

x:Class="iFlexBillAnalyser.App"

xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"

xmlns:System="clr-namespace:System;assembly=mscorlib"

>

<Application.Resources>

<ResourceDictionary>

<Style x:Key="Texted" TargetType="TextBox">

<Setter Property="IsReadOnly" Value="True" />

<Setter Property="BorderThickness" Value="0" />

<Setter Property="Background" Value="Transparent" />

</Style>

 

<Style x:Key="OnlySelectedTextBox" TargetType="TextBox">

<Setter Property="IsReadOnly" Value="True"/>

 

<Setter Property="BorderThickness" Value="0"/>

<Setter Property="Background" Value="Transparent"/>

<Setter Property="Foreground" Value="#FF000000"/>

<Setter Property="Padding" Value="2"/>

<Setter Property="BorderBrush" Value="Transparent"/>

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="TextBox">

<Grid x:Name="RootElement">

<Border x:Name="Border" Opacity="1" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="1">

<ScrollViewer x:Name="ContentElement" BorderThickness="0" IsTabStop="False" Padding="{TemplateBinding Padding}"/>

</Border>

</Grid>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

 

 

 

 

 

 

</ResourceDictionary>

</Application.Resources>

</Application>

______________________________________________

in your xalm

<TextBox Text="LaLa" Style="{StaticResource OnlySelectedTextBox}"/>

iliya tretyakov
iliya tr...

Member

Member

38 points

33 Posts

Re: Re: Re: preventing greyed-out effect when TextBox.IsReadOnly = true

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities