locked
UserControl style afecting its children controls

    Question

  • Hello,

    is there a way to set style of sub controls inside a user control?

    I looked in the web and found this and found Style.Resources but it doesnt seam to be avaialbe in store apps...

    My intent was to make a style and set it to the usercontrol and then that style also influencing the child controls, so i dont have to create multiple styles and setting them individually to each control inside.


    Zead

    Tuesday, June 24, 2014 1:12 PM

Answers

  • Thanks for the answer. I understand your sugestion and i am aware of implicit setting, using only type without key. The problem is that the way sugested must be placed inside the UserControl, i want something more dynamic.

    I wanted to do something like, in main program:

    <ResourceDictionary>
    <Style x:key="controlx" TargetType="controlxtype">
    ... set control style, + sub control style ...
    </Style>
    </ResourceDictionary

    My intent is to do a UserControl in a separate assembly with default properties, use it on main application, merge ResourceDictionaries, and set a style to the user control and change its default apearance, including the subcontrols.

    Sorry if my question wasnt very clear.


    Zead


    You don't need to place the style inside the UserControl.xaml file, you could add the resources to the instance of the user control that you define in the XAML markup of the hosting Page:

      <local:UserControl2>
       <local:UserControl2.Resources>
        <Style TargetType="Button">
         ....
        </Style>
       </local:UserControl2.Resources>
      </local:UserControl2>
    

    or

    		<local:UserControl2>
    			<local:UserControl2.Resources>
    				<ResourceDictionary Source="style.xaml"></ResourceDictionary>
    			</local:UserControl2.Resources>
    		</local:UserControl2>

    • Marked as answer by Zead Tuesday, June 24, 2014 2:24 PM
    Tuesday, June 24, 2014 2:20 PM

All replies

  • Can't you add a style to the <UserControl.Resources>-section and specify only a TargetType for it and no x:Key attribute? For example, if you want to apply a style for all Button elements inside the user control:

    <UserControl......>
    <UserControl.Resources>
        <Style TargetType="Button">
            <Setter Property="BorderThickness" Value="5" />
            <Setter Property="Foreground" Value="Blue" />
            <Setter Property="BorderBrush" >
                <Setter.Value>
                    <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                        <GradientStop Color="Yellow" Offset="0.0" />
                        <GradientStop Color="Red" Offset="0.25" />
                        <GradientStop Color="Blue" Offset="0.75" />
                        <GradientStop Color="LimeGreen" Offset="1.0" />
                    </LinearGradientBrush>
                </Setter.Value>
                </Setter>
            </Style>
    </UserControl.Resources>
    ...
    </UserControl>

    Please refer to the following link for more information: http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh465381.aspx#applying_an_implicit_or_explicit_style_

    Tuesday, June 24, 2014 1:59 PM
  • Thanks for the answer. I understand your sugestion and i am aware of implicit setting, using only type without key. The problem is that the way sugested must be placed inside the UserControl, i want something more dynamic.

    I wanted to do something like, in main program:

    <ResourceDictionary>
    <Style x:key="controlx" TargetType="controlxtype">
    ... set control style, + sub control style ...
    </Style>
    </ResourceDictionary

    My intent is to do a UserControl in a separate assembly with default properties, use it on main application, merge ResourceDictionaries, and set a style to the user control and change its default apearance, including the subcontrols.

    Sorry if my question wasnt very clear.


    Zead

    Tuesday, June 24, 2014 2:12 PM
  • Thanks for the answer. I understand your sugestion and i am aware of implicit setting, using only type without key. The problem is that the way sugested must be placed inside the UserControl, i want something more dynamic.

    I wanted to do something like, in main program:

    <ResourceDictionary>
    <Style x:key="controlx" TargetType="controlxtype">
    ... set control style, + sub control style ...
    </Style>
    </ResourceDictionary

    My intent is to do a UserControl in a separate assembly with default properties, use it on main application, merge ResourceDictionaries, and set a style to the user control and change its default apearance, including the subcontrols.

    Sorry if my question wasnt very clear.


    Zead


    You don't need to place the style inside the UserControl.xaml file, you could add the resources to the instance of the user control that you define in the XAML markup of the hosting Page:

      <local:UserControl2>
       <local:UserControl2.Resources>
        <Style TargetType="Button">
         ....
        </Style>
       </local:UserControl2.Resources>
      </local:UserControl2>
    

    or

    		<local:UserControl2>
    			<local:UserControl2.Resources>
    				<ResourceDictionary Source="style.xaml"></ResourceDictionary>
    			</local:UserControl2.Resources>
    		</local:UserControl2>

    • Marked as answer by Zead Tuesday, June 24, 2014 2:24 PM
    Tuesday, June 24, 2014 2:20 PM