2014-03-04 136 views
0

由于某种原因,我做了CustomButton班。 我试图将默认的Button样式应用于CustomButton为什么不应用默认样式?

<my:CustomButton Style={StaticResource {x:Type Button}}/> 

但这不起作用。你觉得为什么?


<Button x:Name="button1" /> 
<CustomButton Style={Binding Style, ElementName=button1} /> 

这种运作良好。但我认为这不是很好。

回答

0

您必须为您的按钮创建样式并应用样式。

如果删除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.