locked
The resource couldnot be resolved in WPF Style RRS feed

  • Question

  • Resource couldnot be resolved error while creating styles in WPF

    Here is the code 

    <Window x:Class="Sample001.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
        <Window.Resources>
            <Style x:Name="btnStyle" TargetType="Button">
                <Setter Property="Foreground" Value="Red"/>
                <Style.Triggers>
                    <Trigger Property="Control.IsMouseOver" Value="True">
                        <Setter Property="Foreground" Value="green"/>
                    </Trigger>
                </Style.Triggers>            
            </Style>
            
        </Window.Resources>
        <Grid>
            <ListBox Height="246" HorizontalAlignment="Left" Margin="12,12,0,0" Name="listBox1" VerticalAlignment="Top" Width="314" />
            <TextBox Height="32" HorizontalAlignment="Left" Margin="132,272,0,0" Name="textBox1" VerticalAlignment="Top" Width="206" />
            <Button Content="Button" Style="{StaticResource btnStyle}" Height="23" HorizontalAlignment="Left" Margin="344,276,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
            <Button Content="Button"Style="{StaticResource btnStyle}" Height="23" HorizontalAlignment="Left" Margin="360,92,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
        </Grid>
    </Window>



    MSDN Forum


    • Edited by Amal E S Wednesday, October 3, 2012 11:47 AM
    Wednesday, October 3, 2012 11:45 AM

Answers

  • Hi Amal

    Tiny problem with your xaml. Replace x:Name of the style with x:Key, like so:

    <Style x:Key="btnStyle" TargetType="Button">

    Let me know if you don't understand.

    • Proposed as answer by Martin Lottering Thursday, October 11, 2012 6:26 PM
    • Marked as answer by Amal E S Friday, October 12, 2012 12:20 PM
    Thursday, October 11, 2012 6:26 PM

All replies

  • Hi Amal

    Tiny problem with your xaml. Replace x:Name of the style with x:Key, like so:

    <Style x:Key="btnStyle" TargetType="Button">

    Let me know if you don't understand.

    • Proposed as answer by Martin Lottering Thursday, October 11, 2012 6:26 PM
    • Marked as answer by Amal E S Friday, October 12, 2012 12:20 PM
    Thursday, October 11, 2012 6:26 PM
  • Thanks Martin 

    The code works fine now.


    MSDN Forum

    Friday, October 12, 2012 12:20 PM