locked
Problem with ResourceDictionary RRS feed

  • Question

  • Hi

    I'm trying to attach a style (in my code behind) to a StackPanel. The style is defined in ResourceDictionary in a separate file named: Styles.xaml which is stored in 'resource' folder in the project.

    My code behind:

     StackPanel ala = new StackPanel();
                ResourceDictionary resourceDictionary = new ResourceDictionary();
                resourceDictionary.Source = new Uri("/MainContainer;component/resources/Styles.xaml", UriKind.Relative);
                Style style = (Style)resourceDictionary["WindowContentStyle"];
                ala.Style = style;


    Style.xaml:

    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
            <Style TargetType="StackPanel" x:Key="WindowContentStyle">
            <Setter Property="Background" Value="Azure" />
            <Setter Property="Width" Value="250"/>
            <Setter Property="Height" Value="250"/>
            
        </Style>
    
    </ResourceDictionary>


     

     I'm receiving an exception: Error HRESULT E_FAIL has been returned from a call to a COM component. on line:

     resourceDictionary.Source = new Uri("/MainContainer;component/resources/Styles.xaml", UriKind.Relative);
    


    Any ideas how to fix it. The 'build action' for Styles.xaml is set to 'Content'?

    thanks!

    pete

     

     

     

     

     

    Thursday, January 27, 2011 8:56 AM

Answers

  • I think the build action should be Resource.

    Thursday, January 27, 2011 1:46 PM

All replies

  • I think the build action should be Resource.

    Thursday, January 27, 2011 1:46 PM
  • Add that Style file to your Application.Resources by doing this:

    <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>               
                    <ResourceDictionary Source="resources/Styles.xaml"/>  <!-- Assume the resources folder is under your Main Silverlight project -->

                   ...

                 </ResourceDictionary.MergedDictionaries>

              </ResourceDictionary>

    </Application.Resources>


    Now you can use this style either in XAML or in code:

    ala.Style = Application.Current.Resources["WindowContentStyle"] as Style;



             

    Thursday, January 27, 2011 1:53 PM
  • I think the build action should be Resource.

     

    thanks, that worked

    Friday, January 28, 2011 6:53 AM