WPF and external CSS file

Is it possible to store style definitions in a single file (XXXXX.css) and then use that file in XAML files?

+2


source to share


1 answer


It's not called CSS, but you can create a resource dictionary in a separate file like this:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="MyStyledButton" TargetType="Button">
        .
        Put the details of your style here, just like you would for a normal style.
        .
    </Style>

</ResourceDictionary>

      

Then you can import the resource dictionary into other XAML files by setting the resources in the class, for example, if you have a Window class, you would do:



<Window.Resources>
    <ResourceDictionary Source="MyStylesFile.xaml" />
</Window.Resources>

      

This style now takes effect for any control in the window, just as if you specified the style directly in the window resources.

+10


source







All Articles