2009-04-16 10 views
0

Je veux utiliser le même style pour tous les Image s et AutoGreyableImage s (mon contrôle personnalisé qui hérite de Image). J'ai le style suivant déclaré l'application à l'échelle:Comment hériter des styles basés sur le type dans WPF?

<Style TargetType="{x:Type Image}" 
    x:Key="ImageType"> 
    <Setter Property="Stretch" 
      Value="Uniform" /> 
    <Setter Property="Height" 
      Value="16" /> 
    <Setter Property="Width" 
      Value="16" /> 
    <Setter Property="SnapsToDevicePixels" 
      Value="True" /> 
</Style> 

Mais les AutoGreyableImage s n'accepte pas le style. Cela ne fonctionne pas non plus:

<Style TargetType="{x:Type my:AutoGreyableImage}" 
     BasedOn="{DynamicResource ImageType}" /> 

Quelle est la bonne façon de procéder?

Répondre

3

Vous devez utiliser une référence StaticResource dans le style dépend.

Essayez ceci:

<Style TargetType="{x:Type my:AutoGreyableImage}" 
     BasedOn="{StaticResource ImageType}" /> 
3

Cela fonctionne très bien pour moi.

AutoGreyableImage.cs:

public class AutoGreyableImage : Image 
{ 
    public static readonly DependencyProperty CustomProperty = DependencyProperty.Register("Custom", 
     typeof(string), 
     typeof(AutoGreyableImage)); 

    public string Custom 
    { 
     get { return GetValue(CustomProperty) as string; } 
     set { SetValue(CustomProperty, value); } 
    } 
} 

Window.xaml:

<Window.Resources> 
    <Style TargetType="Image" x:Key="ImageStyle"> 
     <Setter Property="Stretch" Value="Uniform"/> 
    </Style> 

    <Style TargetType="{x:Type local:AutoGreyableImage}" BasedOn="{StaticResource ImageStyle}"> 
     <Setter Property="Custom" Value="Hello"/> 
     <Setter Property="Width" Value="30"/> 
    </Style> 
</Window.Resources> 
<Grid> 
    <local:AutoGreyableImage Source="C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Winter.jpg"/> 
</Grid> 
Questions connexes