Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Changing a button's style in runtime ?
15 replies. Latest Post by Hergerger on November 17, 2009.
(0)
nosuic
Member
515 points
133 Posts
09-06-2008 7:12 PM |
In XAML I have 2 styles for buttons (in UserControl.Resources tag) and 1 button. The first style is already applied to my button and what i want now is to change its style (to the second one) when it's clicked. I was trying the following with no luck:
private void onClick(object sender, RoutedEventArgs e) { Button bt = sender as Button; bt.Style = (Style)FindName("activated"); // 1st way //bt.SetValue(Button.StyleProperty, FindName("activated")); 2nd way }
activated is the name of the second style.
i've been thinking that it might not be possible, i've tried so many ways..do you have any ideas? what am i doing wrong ?
thanks a lot in advance! 8-)
sladapter
All-Star
17441 points
3,172 Posts
09-06-2008 10:54 PM |
If you want to set the style defined in the UserControl.Resources you do this:
bt.Style = (Style)this.Resources["activated"];
But you can only do this (apply style to a control) once in code.
09-07-2008 5:36 AM |
I get again the same error "Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))"
sladapter:But you can only do this (apply style to a control) once in code.
Trying to change a button's style on runtime, when this button has already a style, might be the second time the code is trying to apply a style to it.
So it's not possible actually..
Thanks for helping :)
09-07-2008 10:22 AM |
That might be the case.
Another way to dynamically change a style is to change the Style definition itself. You can dynamically import the style xaml and set the this.Resources["buttonStyle"] = XamlReader.Load(newStyleXamlString) as Style. This way you do not reset control's style, but control will take the new style because you changed style definition.
see this blog:
http://leeontech.wordpress.com/2008/07/21/dynamic-styles/
and this thread:
http://silverlight.net/forums/p/21371/74796.aspx#74796
09-07-2008 10:58 AM |
Actually, changing the style itself was my first idea, but I found it too complicated. The fact that the trick with 2 styles isn't working leaves no choice which way to follow.
Thanks very much for the links, i never thought that somebody would like to change a style on runtime until I checked those links ;-P
Thanks again! :)
09-08-2008 10:09 AM |
Actually, for your purpose, using ToggleButton and define Checked state might be a simpler approach than define 2 styles for Button.
See this thread:
http://silverlight.net/forums/t/24461.aspx
09-08-2008 11:03 AM |
that's correct.. it's a big deal to follow the best way and use the right controls from the begining of a project...
codism
372 points
121 Posts
09-08-2008 11:27 AM |
I just want to throw an idea here: if a control's style cannot be assigned twice, there may be a work around: just take the old control out and create a new control and add it to the parent control. Putting the new control in the old control's place may be a little tricky but should be doable.
microsof...
Contributor
2890 points
564 Posts
09-09-2008 12:11 AM |
Use the following:
davjanks
8 points
6 Posts
07-04-2009 1:43 AM |
Hi I know this is quite a while later but...
I have tried the code sample above and it does not set my style at runtime. Here is my code
Button
Content = itemDescription,
};
One difference is that my style is in the app.xaml not local resource. Any ideas?
07-04-2009 11:42 AM |
If the style is defined in App.xmal, use
Style = Application.Current.Resources["redButton"] as Style;
egoZd
326 points
125 Posts
07-05-2009 1:30 AM |
Please view this post to find out how to dymatic set up style for button
http://www.slblogger.cn/p60.htm
07-06-2009 2:44 AM |
Thanks sladapter
Worked like a charm.
Harjinde...
12 points
41 Posts
08-17-2009 3:45 AM |
Hi Sladapter,
I tried as u said that picking up style from external xml file. i created a file consisting of style that I assign to a button at runtime which is already having a style defined in xaml. my styl file is as follows . I am getting error when I use gradient color or storyboard Present in Mouse over, pressed etc,i am also unable to chnge the content If the content is already assigned in the xaml file. i am using web service to read my style file.
<
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFA3AEB9" Offset="0"/>
<GradientStop Color="#FF8399A9" Offset="0.375"/>
<GradientStop Color="#FF718597" Offset="0.375"/>
<GradientStop Color="#FF617584" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="BackgroundAnimation" Storyboard.TargetProperty="Opacity">
<SplineDoubleKeyFrame KeyTime="0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
<SplineColorKeyFrame KeyTime="0" Value="#7FFFFFFF"/>
</ColorAnimationUsingKeyFrames>
</
<ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
<SplineColorKeyFrame KeyTime="0" Value="#D8FFFFFF"/>
<SplineColorKeyFrame KeyTime="0" Value="#3FFFFFFF"/>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="Opacity">
<SplineDoubleKeyFrame KeyTime="0" Value=".55"/>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Opacity">
My Web service code is as follows.....
public XmlDocument GetStyle(String Filename) { String st = Server.MapPath("Styles/" + Filename + ".xml"); if (File.Exists(Server.MapPath("Styles/" + Filename + ".xml"))) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(Server.MapPath("Styles/" + Filename + ".xml")); return xmlDoc; } else return null; }
and I called this method in my silverlight application as......
void btnShowAllConvos_Click(object sender, RoutedEventArgs e) { Button btnRef = null; try { btnRef = e.OriginalSource as Button; if (btnRef != null) { // btnRef.Background = new SolidColorBrush(Colors.Blue); // objJakayaServices.GetStyleDocumentAsync("MyStyles.xml"); objstyle.GetStyleAsync("MyStyles"); objstyle.GetStyleCompleted += new EventHandler(objstyle_GetStyleCompleted); } CurrentConvoType = ConvoType.All; objJakayaServices.GetLoggedUsersConvoListAsync(this.UserName, CurrentConvoStatus, CurrentConvoType, this.CurrentTimeSpan); } catch (Exception ex) { } finally { btnRef = null; } } void objstyle_GetStyleCompleted(object sender, JakayaConvo.MyTestStyleService.GetStyleCompletedEventArgs e) { try { if (e.Error == null && e.Result != null) { Style dynamicstyle = System.Windows.Markup.XamlReader.Load((e.Result as System.Xml.Linq.XElement).ToString()) as Style; if (dynamicstyle != null) { foreach (UIElement objUielement in this.maingrid.Children.Where(u => u.GetType().ToString() == "JakayaConvo.barcontrol")) { barcontrol objBarControl = null; try { objBarControl = objUielement as barcontrol; objBarControl.btnall.Style = dynamicstyle; } catch (Exception) { } finally { objBarControl = null; } } } } } catch (Exception ex) { } finally { } }
salonVB
4 points
09-14-2009 2:35 AM |
Don't make things too complicated.
Do one thing, when u apply first time write this:
And when u want to reapply the style, u can search for the key in Resources and if exists then remove the key and readd the new style. Like following :
where "ActivatedStyleString" is the new style tag string.
This is Silverlight's feature, that u can't apply style twice to a control, u need to remove the style key and reapply the style.
Hergerger
2 points
1 Posts
11-17-2009 10:23 AM |
@salonVB Hi salonVB. I had the same id as you, but the button will not change its appearence. Is there anything else to do (e.g. invalidate, update layout, etc.) or should it be enough? Thankx in advance Hergerger