Skip to main content

Microsoft Silverlight

Answered Question How to access Style in Generic.xaml?RSS Feed

(0)

R3al1ty
R3al1ty

Member

Member

185 points

72 Posts

How to access Style in Generic.xaml?

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
<
Style x:Key="ToolbarButtonStyle" TargetType="Button">
<Setter Property="Background" Value="#FF1F3B53"/>
</Style >

Toolbar.cs
protected
void UpdateView()
{
foreach (Control c in this.ToolbarItems)
{
Button button = c as Button;
if (button!=null)
{
button.Style = this.Resources["ToolbarButtonStyle"] as Style;
}
_itemHost.Children.Add(c);
}

HarshBardhan
HarshBar...

Star

Star

9908 points

1,719 Posts

Re: How to access Style in Generic.xaml?

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.

You have to create object of Generic class and then you have to access that Resource..

If you are creating a Custom Control you can set default style like this in its constructor.

 DefaultStyleKey = typeof( customControl);

 

Mark as answer if this post answered your question.

Harsh Bardhan

sladapter
sladapter

All-Star

All-Star

17439 points

3,172 Posts

Re: How to access Style in Generic.xaml?

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.

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

R3al1ty
R3al1ty

Member

Member

185 points

72 Posts

Re: Re: How to access Style in Generic.xaml?

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.

HarshBardhan
HarshBar...

Star

Star

9908 points

1,719 Posts

Answered Question

Re: Re: How to access Style in Generic.xaml?

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...

Mark as answer if this post answered your question.

Harsh Bardhan

sladapter
sladapter

All-Star

All-Star

17439 points

3,172 Posts

Answered Question

Re: Re: How to access Style in 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.

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

R3al1ty
R3al1ty

Member

Member

185 points

72 Posts

Re: Re: Re: How to access Style in Generic.xaml?

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)

sladapter
sladapter

All-Star

All-Star

17439 points

3,172 Posts

Re: Re: Re: How to access Style in Generic.xaml?

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.

            ...

         }

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities