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}" />