仅当该控件位于具有特定样式的另一个控件内时,才将样式应用于Silverlight控件

时间:2011-07-20 05:26:00

标签: silverlight xaml styles

我有一个网格控件,其中每行包含一个堆栈面板,每个堆栈面板包含一个或多个文本块(而不是问题的核心,如果有更好的方法来实现自定义的文本块网格 - 即“标题行”标签:内容“,我很感激一些提示)

无论如何......我想要一个标题行,其中stackpanel有一个深色背景,textblock有白色粗体文本,然后每个其他行有黑色文本。请注意,只有第一行是使用Style HeaderRow定义的。我已经使用“BasedOn”来定义标题行中只有文本块应该是粗体/白色,但是我发现这也会影响其他行中的所有文本块(没有定义其他样式)。

我实际上希望能够做到

示例XAML

样式:

 <Style x:Key="TitleLabel" TargetType="TextBlock">
                    <Setter Property="FontFamily" Value="Verdana"/>
                    <Setter Property="Margin" Value="5 0 0 0"/>
                    <Setter Property="Width" Value="105"/>
                    <Setter Property="FontWeight" Value="Bold"/>
                </Style>
                <Style x:Key="AlternatingRow" TargetType="StackPanel">
                    <Setter Property="Background" Value="#f0f1ff"/>
                </Style>
                <Style x:Key="HeaderRow" TargetType="StackPanel">
                    <Setter Property="Background" Value="#666666"/>
                </Style>
                <Style TargetType="TextBlock" BasedOn="StaticResource HeaderRow" >
                    <Setter Property="Foreground" Value="White"/>
                    <Setter Property="FontWeight" Value="Bold"/>
                </Style>

XAML

<Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="auto"/>
                    </Grid.RowDefinitions>
                    <StackPanel Orientation="Horizontal" Grid.Row="0" Style="{StaticResource HeaderRow}">
                        <TextBlock Text="Header Row" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Grid.Row="1" Style="{StaticResource AlternatingRow}">
                        <TextBlock Text="HeaderLabel:" Style="{StaticResource TitleLabel}" />
                        <TextBlock Text="Content" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Grid.Row="2">
                        <TextBlock Text="HeaderLabel"  Style="{StaticResource TitleLabel}" />
                        <TextBlock Text="Content" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Grid.Row="3" Style="{StaticResource AlternatingRow}">
                        <TextBlock Text="HeaderLabel"  Style="{StaticResource TitleLabel}" />
                        <TextBlock Text="Content" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Grid.Row="4">
                        <TextBlock Text="HeaderLabel"  Style="{StaticResource TitleLabel}" />
                        <TextBlock Text="Content" />
                    </StackPanel>
                </Grid>

1 个答案:

答案 0 :(得分:0)

您没有正确使用Style BasedOn属性。它所做的只是表明一种风格'延伸'另一种风格,即它复制了所有的设定值。 (注意,您的示例也会失败,因为您尝试在TargetTypes不兼容的另一个上基于样式)它并不表示当一个元素嵌套在另一个元素中时应用了样式。

不幸的是,Silverlight没有您需要的功能,您无法根据可视树中的元素位置进行样式设置。您将必须明确地为每个TextBlock设置样式。

尽管如此,我确实创建了一种使用CSS进行样式设置的机制:

http://www.scottlogic.co.uk/blog/colin/2009/03/using-css-selectors-for-styling-in-wpf/

这允许您根据父元素创建选择器。

相关问题