WPF ResourceDictionary and DynamicResource

Posted by lanicor on Stack Overflow See other posts from Stack Overflow or by lanicor
Published on 2011-12-13T22:50:58Z Indexed on 2012/06/30 3:16 UTC
Read the original article Hit count: 234

Filed under:
|
|

i am new to WPF so maybe this will be "noob" question but i can't find answer to it (and it seems so easy)...

well i am programing WPF app and i want to use smth similar to css for webpages, so i found ResourceDictionary and tried to use it...

I have main window in which i create Frame and on click some UserControl is loaded to that same Frame.I include my ResourceDictionary (root/style/Styles.xaml) to my main window like this:

<Window.Resources>
    <ResourceDictionary x:Key="styles" Source="style/Styles.xaml" />
</Window.Resources>  

in which i have:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Style x:Key="ProductsRequired" TargetType="{x:Type Label}">
    <Setter Property="Height" Value="28" />
    ...

I don't use it on my main window but want to use it on my UserControls, which are loaded to that main window and i try to use it like this:

<Label Name="product1" Style="{DynamicResource ProductsRequired}"  />

Warning i am getting all the time is: The resource "ProductsRequired" could not be resolved.

I even tried including Styles to my UserControl

<UserControl.Resources>
  <ResourceDictionary x:Key="styles" Source="../style/Styles.xaml" />
</UserControl.Resources>

and nothing happens...

Well my question is long but i wanted to be clear... :)

P.S. all that logic works in C# code btw

private ResourceDictionary myStyles = new ResourceDictionary();
Style ProductsRequired = myStyles["ProductsRequired"] as Style;
product1.Style = ProductsRequired;

© Stack Overflow or respective owner

Related posts about wpf

Related posts about xaml