none
StaticResource does not work in Generic.xaml RRS feed

  • Question

  • Hi i'm creating a style for my custom control, so I write style in Generic.xaml.

    I wanted to share some parts betwen two controls. So I've created style and tried to use it using StaticResource in another but that caused exeption. I've tried to move style to Style.Resources of Style for my controll but no difference. Here is my code:

     

     <Style TargetType="{x:Type local:MyCustomControl}">
     <Style.Resources>
      <Style TargetType="{x:Type FrameworkElement}" x:Name="ItemContainerStyle">
      <Setter Property="ToolTip" Value="Test">
      </Setter>
      </Style>
     </Style.Resources>
     <Setter Property="ItemContainerStyle" Value="{StaticResource ItemContainerStyle}"> </Setter>
    </Style>
    

     

    I want to know what I done wrong and why it is happening. 

    Just to mention folowing code work just fine:

     

     <Style TargetType="{x:Type local:MyCustomControl}">
    <Setter Property="ItemContainerStyle" >  
      <Setter.Value>
      <Style TargetType="{x:Type FrameworkElement}" x:Name="ItemContainerStyle">
       <Setter Property="ToolTip" Value="Test">
       </Setter>
      </Style> 
      </Setter.Value>
     </Setter>
    </Style>
    

     

    Friday, May 14, 2010 12:54 PM

Answers

  • For Style to apply from Generic.xaml Please change from x:Name to x:Key and then try. Please make sure that Generic.xaml is added in app.xaml

     

     <Style TargetType="{x:Type local:MyCustomControl}">
    <Setter Property="ItemContainerStyle" > 
      <Setter.Value>
      <Style TargetType="{x:Type FrameworkElement}" x:Key="ItemContainerStyle">
       <Setter Property="ToolTip" Value="Test">
       </Setter>
      </Style>
      </Setter.Value>
    </Setter>
    </Style>

    • Marked as answer by SerrNovik Saturday, May 15, 2010 5:39 AM
    Friday, May 14, 2010 3:38 PM

All replies

  • For Style to apply from Generic.xaml Please change from x:Name to x:Key and then try. Please make sure that Generic.xaml is added in app.xaml

     

     <Style TargetType="{x:Type local:MyCustomControl}">
    <Setter Property="ItemContainerStyle" > 
      <Setter.Value>
      <Style TargetType="{x:Type FrameworkElement}" x:Key="ItemContainerStyle">
       <Setter Property="ToolTip" Value="Test">
       </Setter>
      </Style>
      </Setter.Value>
    </Setter>
    </Style>

    • Marked as answer by SerrNovik Saturday, May 15, 2010 5:39 AM
    Friday, May 14, 2010 3:38 PM
  • Obvious. Thank you Mustehsan. How could I forget...
    Saturday, May 15, 2010 5:40 AM