我创建了一个自定义按钮,因为我想要其中的图像和文本,如下所示:
<Style TargetType="{x:Type Local:ImageButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Local:ImageButton}">
<StackPanel Height="Auto" Orientation="Horizontal">
<Image Margin="0,0,3,0" Source="{TemplateBinding ImageSource}"/>
<TextBlock Text="{TemplateBinding Content}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这里,ImageButton 是一个继承自 Button 类的类,并以 ImageSource 作为依赖属性。
但我想保留原始按钮的外观和感觉。我该怎么做?
最佳答案
您可以使用Style.BasedOn
属性:
<Style TargetType="{x:Type Local:ImageButton}" BasedOn="{StaticResource {x:Type Button}}">
<!-- ... -->
</Style>
关于WPF 在自定义控件中保留基本样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2728904/