2009-04-16 19 views
0

ImageAutoGreyableImage(すべてImageから継承するカスタムコントロール)に同じスタイルを使用したいと思います。私は、次のスタイルは、アプリケーション全体の宣言があります。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> 

しかしAutoGreyableImage sがスタイルを受け入れません。これはどちらでも動作しません:

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

これを行う正しい方法は何ですか?

答えて

3

あなたは依存のスタイルでStaticResource参照を使用する必要があります。

これを試してみてください:

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

私にとってはうまく動作します。

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> 
関連する問題