开发者

Silverlight: Why does this style work in one place but not another?

开发者 https://www.devze.com 2023-01-30 14:01 出处:网络
I have a style in my Application.Resources: <Style x:Name=\"TransparentBackground\" TargetType=\"Border\">

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).

  1. Set by coercion by the property system.
  2. Set by active animations or held animations.
  3. Set locally, either by code, by direct setting in XAML, or through data binding.
  4. Set by the TemplatedParent.
  5. Implicit style—this applies only to the Style property.
  6. Set by Style triggers.
  7. Set by Template triggers.
  8. Set by Style setters.
  9. Set by the default Style.
  10. Set by inheritance.
  11. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消