Search code examples
c#wpfwindowsvisual-studioxaml

Why is Visual Studio showing an error saying Windows.Resources is not found in the type Window?


I am a beginner in WPF applications. I have created a WPF application and I want to use a Main Page in my application. Inside the Window scope, I want to declare Window.Resources but it returns two errors:

1- The member Resources is not recognized or accessible.
2- The attachable property Resources wasn't found in the type Window.

What could be missing in this case?

Here's my XAML code:

<Window x:Class="ArmsPosition.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
        xmlns:local="clr-namespace:ArmsPosition"
        mc:Ignorable="d"
        Loaded="Window_Loaded"
        Closing="Window_Closing"        
        WindowState="Normal"
        Title="Arms Positions" Height="560" Width="800" MinHeight="560" MinWidth="800" MaxHeight="560" MaxWidth="800">
    <Grid>
        <Window.Resources>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="FontFamily" Value="Segoe UI"/>
                <Setter Property="FontSize" Value="20"/>
                <Setter Property="Foreground" Value="#FF999999"/>
            </Style>
        </Window.Resources>

    </Grid>
</Window>  

Solution

  • It's because you are using Windows.Resource within the Grid element. Resource is a direct child property of the Window element.

    <Window x:Class="ArmsPosition.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:ArmsPosition"
            mc:Ignorable="d"
            Loaded="Window_Loaded"
            Closing="Window_Closing"        
            WindowState="Normal"
            Title="Arms Positions" Height="560" Width="800" MinHeight="560" MinWidth="800" MaxHeight="560" MaxWidth="800">
        <Window.Resources>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="FontFamily" Value="Segoe UI"/>
                <Setter Property="FontSize" Value="20"/>
                <Setter Property="Foreground" Value="#FF999999"/>
            </Style>
        </Window.Resources>
        <Grid>
            <!--SOME CONTROLS HERE-->
        </Grid>
    </Window>