Implicit xaml style overwritten by explicit style
I Have this very simple xaml style problem in Silverlight4.
When I set a style on element through explicit styling my implicit style is removed? I created a simple example to illustrate the problem.
<UserControl.Resources>
<Style TargetType="TextBox">
<Setter Property="Background" Value="red"/>
</Style>
<Style TargetType="FrameworkElement" x:Key="test">
<Setter Property="Margin" Value="20"/>
</Style>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White"&开发者_如何学编程gt;
<TextBox Style="{StaticResource test}" Height="40" Width="120"> </TextBox>
</Grid>
</UserControl>
So when I remove the Style="{StaticResource test}
the implicit style is applied again.
Note: this is oversimplified example. So moving the margin to the texbox style is not an option because the real project contains larger xaml styling.
Does somebody know a solution or can confirm that this isn't possible?
Thx in advance!
Take a look at the BasedOn style property. This will allow you to inherit from a base style, to use the base properties and add some changes.
Now, there is a problem with BasedOn - it can't be used with an implicit style. However, this is pretty easy to work around. This article demonstrates the work around and will also explain the BasedOn property a bit better:
Silverlight how-to: Inherit from an Implicit Style
精彩评论