Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit How to access Style in Generic.xaml?
7 replies. Latest Post by sladapter on October 9, 2008.
(0)
R3al1ty
Member
185 points
72 Posts
10-02-2008 5:57 AM |
Hi all,I have a Style defined in Generic.xaml and I have a custom control called Toolbar. When users add to the Controls collection in the Toolbar, the Toolbar has to apply the default style to each child Control as defined in Generic.xaml. I am not able to programmatically retrieve and apply this Style (pls see Yellow highlighted code). Any ideas? Thanks.
Generic.xaml<
HarshBar...
Star
9908 points
1,719 Posts
10-02-2008 6:12 AM |
Hi,
From your code looks like you are looking for that style in that UserControl Only.
this.Resources["ToolbarButtonStyle"] as Style;//Here this is there.
If you are creating a Custom Control you can set default style like this in its constructor.
DefaultStyleKey = typeof( customControl);
sladapter
All-Star
17439 points
3,172 Posts
10-02-2008 9:53 AM |
Generic.xaml is not the place for you to define styles for the control you used in your application. For that purpose you should put the style definition in App.xaml or the UserControl where you want to use this style. Generic.xaml is for you to define default template for custom controls you wrote.
10-02-2008 10:11 AM |
Harsh, is there a code behind for Generic.xaml? How would I create an instance of the Generic class. I undertand that resources are local to the file but in this case, my control is a Custom Control with the styles defined in Generic.xaml.
Sladapter, as the control designer, I am trying to enforce a default style for all the child elements that are placed within my Toolbar control. For example, if the user drags a Button into my Toolbar control, I want to force the Style of the Button to my default ToolbarButtonStyle. In a sense, this child item style is part of the default style of the control and hence I want to place it in Generic.xaml. It is local to the control assembly.
10-02-2008 10:18 AM |
Hi, There is no code behind for generic.xaml so you can not create instance of that.
For custom control you have to set style by using Defaultstylekey in constructor of your custom control.
If you want to apply different Styles Probably you can create Some style definition similar to Generic.Xaml...
In that case, you might be better off to write a ToobarButton custom control, you do not need to write any code, but just override the default template. You define the Template in the generic.xaml for ToobarButton. In your Toolbar control, you create ToolbarButton instead of generic Button.
10-02-2008 10:33 AM |
Thanks guys, that is in fact the approach I took finally. I was hoping there was another way though and to give my users the ability to add a generic Button. On the plus side, this gives maximum flexibility.
My Generic.xaml is growing, that Button template is quite big haha :)Harsh, so am I right to assume that there is no easy way to read a Style by its Key off Generic.xaml for referencing in C# code in the same control assembly? (Short of parsing it as an XML stream)
10-09-2008 10:29 AM |
Hi, I found this approach also could work if you do not want to write a ToolbarButton custom control:
Define your toolbar button style in the generic.xaml under the Toolbar root element Resources section:
Then at OnApplyTemplate function of your Toolbar, your get the buttonStyle = RootElement.Resources[
public override void OnApplyTemplate() { base.OnApplyTemplate(); Panel RootElement = GetTemplateChild(YourToolbarRootElementName) as Panel; //
Style buttonStyle = RootElement.Resources["ToolbarButton"] as style; // Now you have the style you can assign to the Button.
...
}