Search code examples
wpfxamlstyles

Resource "x" could not be resolved WPF


Im new at WPF and im using some DevExpress controls Im trying to implement an style to some buttons but always shows the error that the Resource cannot be resolved.

MainWindow:

<dx:DXWindow
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
    xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
    xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" x:Class="LicenceManagerWPF.MainWindow"
    xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" dx:ThemeManager.ThemeName="Office2016"
    Title="MainWindow" Height="746.218" Width="1139.154" 
    WindowStartupLocation="CenterScreen">   
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="80"/>
    </Grid.RowDefinitions>
    <StackPanel  x:Name="Stack_Top" Orientation="Horizontal"  Grid.Row="1" >
        <dx:SimpleButton x:Name="btnRefresh" Style="{StaticResource ResourceKey=CustomStyles}"   Width="55" ToolTip="Refresh" Margin="10,10,10,10" Glyph="{dx:DXImage Image=Refresh_32x32.png}" Content="Resfresh" />
        <dx:SimpleButton x:Name="btndNew" Width="55" ToolTip="New Customer" Margin="10" Glyph="{dx:DXImage Image=NewContact_32x32.png}" Content="New Customer"  />
        <dx:SimpleButton x:Name="btnDelete" ToolTip="Delete Customer" Width="55" Margin="10" Content="Delete" Glyph="{dx:DXImage Image=Cancel_32x32.png}"/>
    </StackPanel>

</Grid>

This is the App.xaml

<Application x:Class="LicenceManagerWPF.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml" 
         Startup="OnAppStartup_UpdateThemeName">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary x:Name="CustomStyles" Source="StyleResource.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

This is my Styles file

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:LicenceManagerWPF"
                xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
                xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" x:Class="LicenceManagerWPF.MainWindow"    
                xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core">
<Style x:Name="HeaderButtons" TargetType="dx:SimpleButton">
    <Setter Property="Margin" Value="10"/>
    <Setter Property="Width" Value="55"/>
</Style>

I was searching and everything looks like fine but i dont get it why it cannot be resolved. Regards


Solution

  • There is no need to name ResourceDictionary , just provide its Source:

    <ResourceDictionary Source="StyleResource.xaml"/>
    

    ResourceDictionary items should have x:Key. In case of a Style if there is no explicit key, TargetType will be used as a key. It is a way to create default styles.

    If you want named style, then set x:Key

    <Style x:Key="HeaderButtons" TargetType="dx:SimpleButton">
        <Setter Property="Margin" Value="10"/>
        <Setter Property="Width" Value="55"/>
    </Style>
    

    And finally StaticResource extension references resources by resource key, not by names:

    Style="{StaticResource HeaderButtons}"
    

    Also: when you set a Style for a button, Margin and Width settings (Width="55" Margin="10,10,10,10") becomes redundant. They can be used to override style setting, but in this case they are the same so why write them?