Skip to main content

Microsoft Silverlight

Answered Question is it possible to change style in code behind?RSS Feed

(0)

xiebin
xiebin

Member

Member

2 points

10 Posts

is it possible to change style in code behind?

Hi Friends,

I have set a style for object in front, and i want to change the textblock.text that is inside the style in code behind as i can bind data into textblock. google seems like not working for me this time, there were very few resources i can refer to. Any suggestion would be appreciated in advance!

 

 

Ben

philip.beadle
philip.b...

Member

Member

500 points

104 Posts

Re: is it possible to change style in code behind?

You can set a style once but you cannot change it once it is set.  Im not quite sure what you are asking though. 

xiebin:
i want to change the textblock.text that is inside the style in code behind as i can bind data into textblock

If you change the text it will automatically have whatever style you already set on the control.

Philip Beadle (DotNetNuke Trustee)

My Blog

My DNN Blog

(If this has answered your question, Mark as Answer)

xiebin
xiebin

Member

Member

2 points

10 Posts

Re: is it possible to change style in code behind?

Hi Beadle,

Sorry for the unclear discription of my problem. I have defined a style in front, and i want to create a button in code behind meanwhile set style to the button. is it possible to do that? Thanks

 

Best Regards,

Ben Xie Bin

Jonathan Shen – MSFT
Jonathan...

All-Star

All-Star

24979 points

2,434 Posts

Microsoft
Answered Question

Re: is it possible to change style in code behind?

Hi Xiebin,

First, we define the style in app.xaml.   For example,

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"                  x:Class="AppResStyle.App"                >       <Application.Resources>
             <Style x:Key="ButtonStyle" TargetType="Button">
                     ********
             </Style>
              </Application.Resources>   </Application>

Then, call it on code hide when any event is raised.  See this.

               MyButton.Style = Application.Current.Resources["ButtonStyle"] as Style;

You can get the sample here.

Best regards,

Jonathan

Jonathan Shen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

xiebin
xiebin

Member

Member

2 points

10 Posts

Re: is it possible to change style in code behind?

Hi Jonathan,

Thanks for your reply, Your codes is really helpful!

In my case, i used MyButton.Style = (Style)this.Resources["ButtonStyle"];

It comes from your idea.Big Smile 

XieBin

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities