滑块样式的Thumb的WPF分层(ZIndex)

时间:2014-07-15 09:45:57

标签: c# wpf xaml slider

我有一个自定义的Slider模板,我想放置一个位于Decrease和Repeat按钮上方但在拇指下方的内容。我已经尝试将zindex添加到矩形中,使其显示在所有内容之上,但我无法弄清楚如何使拇指出现在矩形上方

<!-- This is what I want to appear above the repeatbuttons but below the thumb -->
<Rectangle Height="8" Width="8" Fill="Yellow" Grid.Row="1" Panel.ZIndex="1" />

<Track x:Name="PART_Track" Grid.Row="1">
    <Track.DecreaseRepeatButton>
        <RepeatButton Command="{x:Static Slider.DecreaseLarge}" Style="{StaticResource DecreaseSliderRepeatButtonStyle}"/>
    </Track.DecreaseRepeatButton>
    <Track.IncreaseRepeatButton>
        <RepeatButton Command="{x:Static Slider.IncreaseLarge}" Style="{StaticResource IncreaseSliderRepeatButtonStyle}"/>
    </Track.IncreaseRepeatButton>

    <Track.Thumb>
        <Thumb x:Name="Thumb" Style="{StaticResource HorizontalSliderThumbStyle}"/>
    </Track.Thumb>
</Track>

这是拇指的相关风格

<Style x:Key="HorizontalSliderThumbStyle" TargetType="{x:Type Thumb}">
    <Setter Property="Panel.ZIndex" Value="100"/>
    <Setter Property="Focusable" Value="false"/>
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="Foreground" Value="Gray"/>
    <Setter Property="Cursor" Value="Hand"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Thumb}">
                <Border Background="#E6323232" CornerRadius="8" Height="16">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <!--<TextBlock FontFamily="Arial" x:Name="tbPlaybackTime" Margin="7,0,7,0" Text="09:35:00" FontSize="10" Foreground="#eee" Background="Transparent" VerticalAlignment="Center"/>-->
                        <TextBlock FontFamily="Arial" x:Name="tbPlaybackTime" Margin="7,0,4,0" Text="09:35:00" FontSize="10" Foreground="#eee" Background="Transparent" VerticalAlignment="Center"/>
                        <Ellipse Grid.Column="1" Height="16" Width="16" Panel.ZIndex="10000">
                            <Ellipse.Fill>
                                <RadialGradientBrush GradientOrigin="0.5,0" RadiusX="1" Center="0.5,0.5" >
                                    <RadialGradientBrush.GradientStops>
                                        <GradientStop Color="#ffffff" Offset="0.3" />
                                        <GradientStop Color="#cccccc" Offset="0.8" />
                                    </RadialGradientBrush.GradientStops>
                                </RadialGradientBrush>
                            </Ellipse.Fill>
                            <Ellipse.Effect>
                                <DropShadowEffect BlurRadius="3" Color="Black" Direction="270" Opacity="0.8" ShadowDepth="1" />
                            </Ellipse.Effect>
                        </Ellipse>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

1 个答案:

答案 0 :(得分:0)

此处是一种解决方案,用于将自定义内容置于RepeatButton

Slider之上
    <Track Grid.Row="1"
           x:Name="PART_Track">
        <Track.Resources>
            <ControlTemplate TargetType="RepeatButton"
                             x:Key="myRepeatbutton">
                <Grid Background="Transparent">
                    <RepeatButton IsHitTestVisible="False"
                                  Content="{TemplateBinding Content}" 
                                  Style="{TemplateBinding Style}" />
                    <Rectangle Height="8"
                               Width="8"
                               Fill="Yellow" />
                </Grid>
            </ControlTemplate>
        </Track.Resources>
        <Track.DecreaseRepeatButton>
            <RepeatButton Command="Slider.DecreaseLarge"
                          Style="{StaticResource DecreaseSliderRepeatButtonStyle}"
                          Template="{StaticResource myRepeatbutton}" />
        </Track.DecreaseRepeatButton>
        <Track.Thumb>
            <Thumb Style="{StaticResource HorizontalSliderThumbStyle}"/>
        </Track.Thumb>
        <Track.IncreaseRepeatButton>
            <RepeatButton Command="Slider.IncreaseLarge"
                          Style="{StaticResource IncreaseSliderRepeatButtonStyle}"
                          Template="{StaticResource myRepeatbutton}" />
        </Track.IncreaseRepeatButton>
    </Track>

想法是利用Template的{​​{1}}属性,并使用自定义RepeatButton应用所需的外观

还有其他可能的解决方法,使用不透明蒙版等,但正确对齐可能有点复杂。因此,如果上述方法无法按预期方式运作,我们也可以选择。