WPF 在自定义控件中保留基本样式

标签 wpf custom-controls styles

我创建了一个自定义按钮,因为我想要其中的图像和文本,如下所示:

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

相关文章:

WPF 覆盖 setter 属性

WPF滚动和焦点改变问题

android - 如何扩展像 GridView 这样复杂的 Android View ?

.net - WebBrowser(WPF 和 WinForms 控件)和 JSON 对象 - JSON 未定义

android - Spinner 的下拉菜单无法正常工作

iphone - 默认键盘出现在 iPhone 而不是我的自定义键盘

c# - '[未知 ]' property does not point to a DependencyObject in path ' (0).(1)[3].(2)'

css - 在同一页面上使用两种不同样式的复选框

c# - 如何在 Prism wpf 模块化应用程序中首先使用代码动态创建数据库表?

wpf - 平滑调整 UIElement 的大小