locked
How do you assign a style to an element inside a style? RRS feed

  • Question

  • How do you assign a style to an element inside a style?
    Friday, July 24, 2009 1:56 PM

Answers

  • If you are referring to an element inside a ControlTemplate in a Style, you can add a Style to ControlTemplate.Resources, e.g.,

    <Page
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
        <Page.Resources>
            <Style x:Key="StyleKey" TargetType="{x:Type Control}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <ControlTemplate.Resources>
                                <Style TargetType="{x:Type Ellipse}">
                                    <Setter Property="Fill" Value="DarkOrange" />
                                </Style>
                            </ControlTemplate.Resources>
                            <Grid>
                                <Ellipse Height="50" Width="50" />
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Page.Resources>
    
        <Control Style="{StaticResource StyleKey}" />
      
    </Page>
    Wednesday, July 29, 2009 4:57 PM