Silverlight: Why does this style work in one place but not another?
I have a style in my Application.Resources
:
<Style x:Name="TransparentBackground" TargetType="Border">
<Setter Property="Background" Value="AntiqueWhite" />
<Setter P开发者_StackOverflow中文版roperty="Opacity" Value=".6" />
<Setter Property="Padding" Value="5" />
</Style>
I have applied it in two places. In the first, it works:
<UserControl.Resources>
<DataTemplate x:Key="FileTemplate">
<Border Style="{StaticResource TransparentBackground}">
<TextBlock TextWrapping="Wrap">
<Run Text="{Binding Name, FallbackValue='File Name'}" FontWeight="Bold" />
<Run Text="." Foreground="#787878" FontWeight="Light" />
<Run Text="{Binding TypeExtension, FallbackValue='type'}" Foreground="#787878" FontWeight="Light" />
</TextBlock>
</Border>
</DataTemplate>
</UserControl.Resources>
When I that DataTemplate
is rendered, it looks great. However, when I use the style within LayoutRoot
, it fails:
<Border Style="{StaticResource TransparentBackground}">
<TextBlock x:Name="searchResultsFoundCountText" />
</Border>
The background color is grey instead of AntiqueWhite
, and the opacity is 1
instead of .6
. However, the padding seems to be working. Why might this be happening?
I'm using Silverlight 4.
You set properties using style setter. This way has a small priority (8th place).
- Set by coercion by the property system.
- Set by active animations or held animations.
- Set locally, either by code, by direct setting in XAML, or through data binding.
- Set by the TemplatedParent.
- Implicit style—this applies only to the Style property.
- Set by Style triggers.
- Set by Template triggers.
- Set by Style setters.
- Set by the default Style.
- Set by inheritance.
- Set by metadata
But you have a silverlight without triggers and your code doesn't contain templates, so a possible answer may be code setter (3rd) or animation (2nd).
Also your style works well in the root user control MainPage.xaml.
Here is msdn article about property precendence.
精彩评论