miƩrcoles, 7 de mayo de 2008

Changing Skinn at runtime

A lot of post teach how to make template skinks for silverlighr controls but I'did not find a solution to change these skinns at runtime. I saw some post telling that it's not supported at runtime in SL2. So here our solution (notice that this is "like" code since I have problems editing:

1 - In the App.xaml, define the skinn just as a Control Template, NOT like a Style

Coment this lines:
Style x:Key="inniciaiPhoneButtonBlack" TargetType="Button"
Setter Property="IsEnabled" Value="true" ....
Setter Property="Template"
Setter.Value
And change the Control Template tag for
ControlTemplate To
ControlTemplate x:Key="inniciaiPhoneButtonBlack" TargetType="Button"
Then continue with the template definition as usual.

2 - In code:
We asume that you have this button on your page (xaml):
Button Template="{StaticResource mybuttonTemplate} x:Name="mybutton"

So when you run the application your button will render with the (mybuttonTemplate) look and fell, then just do in the code behind:

mybutton.Template = (ControlTemplate)(Application.Current.Resources[inniciaiPhoneButtonBlack]);

...And now your button will have the [inniciaiPhoneButtonBlack] look & fell.

Of course this is just a beginning, then we should figure out how to change the entire application skinn... may be subscribing all our controls to an event?

Hope it's help, fell free to ask for any doubt or to sugest a better way to do this.




0 comentarios: