Maximus
Maximus

Reputation: 1299

How to access a Style that exists in a UserControl?

I have a Style declared within a UserControl. I then placed that UserControl in a Window. How can I access the UserControl's style from the Window's XAML ???

Upvotes: 0

Views: 112

Answers (2)

shanewwarren
shanewwarren

Reputation: 2294

If you give your UserControl a name you can simply use an ElementName binding to get access to the UserControl's Style property.

<Window Title="MainWindow" Height="350" Width="525" 
Style="{Binding ElementName=myUserControl, Path=Style}">
    <UserControl Name="myUserControl" >
        <UserControl.Style>
            <Style TargetType="Control">
                <Setter Property="Background" Value="Yellow" />
            </Style>
        </UserControl.Style>
    </UserControl>
</Window>

If you want to access just an individual setter value from the UserControl's style you can use something like:

<Window Background="{Binding ElementName=myUserControl, Source=Style, Path=Background}">

Hope this helps!

Upvotes: 1

Jonas
Jonas

Reputation: 675

Resources are resolved by going up through the visual tree. This is not a usually way to work with styles.

You can do it by code behind :

Style style = (Style)yourUserControl.Resources[YourKeyStyle];

Upvotes: 2

Related Questions