Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wpf xaml inheritance

Tags:

wpf

xaml

Is it possible to inherit same style for a ControlTemplate in two different windows? I'm new to wpf and I'm not sure how to do it or even if it's possible. For example if I have in Window1.xaml:

<Window.Resources>
    <ControlTemplate x:Key="myStyle" TargetType="Button">
        ...
    </ControlTemplate>
</Window.Resources>

And in Window2.xaml I want to use it like this:

<Grid>
    <Grid.Resources>
        <Style TargetType="Button" BasedOn="{StaticResource myStyle}">
            ...
        </Style>
    </Grid.Resources>
<Grid>

How do I import the style from the first window?

like image 814
Sierra313 Avatar asked Nov 25 '25 20:11

Sierra313


1 Answers

Yes it is possible, you can move style to app.xaml, and both windows will see that style

something like this in app.xaml

<Application.Resources>
 <ResourceDictionary>
    <Style x:Key="myStyle" TargetType="Button">
        ...
    </Style>
    <Style TargetType="Button" BasedOn="{StaticResource myStyle}">
        ...
    </Style>
 </ResourceDictionary>
</Application.Resources>

and both windows will see that style

like image 121
Arsen Mkrtchyan Avatar answered Nov 28 '25 10:11

Arsen Mkrtchyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!