Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在WPF中继承基于类型的样式?_C#_.net_Wpf - Fatal编程技术网

C# 如何在WPF中继承基于类型的样式?

C# 如何在WPF中继承基于类型的样式?,c#,.net,wpf,C#,.net,Wpf,我想对所有图像和自动预览图像使用相同的样式(从图像继承的自定义控件)。我在应用程序范围内声明了以下样式: <Style TargetType="{x:Type Image}" x:Key="ImageType"> <Setter Property="Stretch" Value="Uniform" /> <Setter Property="Height" Value="16" />

我想对所有
图像
自动预览图像
使用相同的样式(从
图像
继承的自定义控件)。我在应用程序范围内声明了以下样式:

<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>
做这件事的正确方法是什么?

对我来说很好

autogeyableimage.cs:

Window.xaml:


它对我来说很好用

autogeyableimage.cs:

Window.xaml:


您必须在依赖样式中使用StaticResource引用

试试这个:

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

您必须在依赖样式中使用StaticResource引用

试试这个:

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

<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>
<Style TargetType="{x:Type my:AutoGreyableImage}" 
       BasedOn="{StaticResource ImageType}" />