Skip to main content
Home Forums Silverlight Programming Programming with .NET - General preventing greyed-out effect when TextBox.IsReadOnly = true
9 replies. Latest Post by iliya tretyakov on September 22, 2009.
(0)
peterdungan
Member
223 points
150 Posts
08-11-2009 8:24 AM |
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
Contributor
5892 points
1,042 Posts
08-11-2009 8:27 AM |
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.
08-12-2009 5:41 AM |
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...
08-12-2009 6:31 AM |
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.
08-12-2009 7:34 AM |
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 CopyGive it a name MyTextBoxStyleClick 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.
08-13-2009 2:35 PM |
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.
08-13-2009 2:43 PM |
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.
08-13-2009 3:17 PM |
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.
iliya tr...
38 points
33 Posts
09-22-2009 11:57 AM |
Let me some abuse style for TextBox only for text selection :)
in App.xaml
<
</
______________________________________________
in your xalm