Search code examples
c#wpfxamlresourcedictionary

C# WPF specify folders in XAML style


I have a project with the following structure:

Proj
├──Views
├   ├──Dashboard.xaml
├   ├──Dashboard.cs
├
├──Styles
    ├──DashboardStyle.xaml

In my DashboardStyle.xaml, I have this code:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:Proj.Styles">

    <Style x:Key="MyWindowStyle" TargetType="local:Proj/Views/Dashboard">
        ....
        ....
    </Style>

</ResourceDictionary>

But it gives the error:

The name "Proj/Views/Dashboard" does not exist in the namespace "clr-namespace:Proj.Styles"

How can I resolve this issue?


Solution

  • Types are referenced using namepace and type name, not via physical file paths.

    So to reference the type Proj.Views.Dashboard, add the corresponding namespace as XML namespace declaration and use it in the TargetType attribute, e.g.

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:Proj.Styles"
                    xmlns:views="clr-namespace:Proj.Views" >
    
        <Style x:Key="MyWindowStyle" TargetType="views:Dashboard">
            ....
            ....
        </Style>
    
    </ResourceDictionary>