您必须为您的按钮创建样式并应用样式。
如果删除x:Key="ButtonStyle1"
那么如果你的按钮样式包含
<Style x:Key="ButtonStyle1" TargetType="{x:Type yourNameSpace:CustomButton}">
<Setter Property="Foreground"
Value="Red" />
<Setter Property="Margin"
Value="10" />
</Style>
<yourNameSpace:CustomButton Width="100" Height="100" Style="{StaticResource ButtonStyle1}"> </yourNameSpace:CustomButton>
您应该检查Styling and Templating你的风格将被应用到所有的CustomButton
,当然这Tutorial
Style={StaticResource {x:Type Button}}
这是错误的是期待一个资源不是一个类型的关键。
从技术文档StaticResource:
Provides a value for any XAML property attribute by looking up a reference to an already defined resource. Lookup behavior for that resource is analogous to load-time lookup, which will look for resources that were previously loaded from the markup of the current XAML page as well as other application sources, and will generate that resource value as the property value in the run-time objects.