【问题标题】:How can I add a thumb to a slider without losing the slider tap behavior?如何在不丢失滑块点击行为的情况下向滑块添加拇指?
【发布时间】:2010-11-09 08:17:02
【问题描述】:

这个问题可能听起来很奇怪,就这样吧:

silverlight 3 for windows phone 中的滑块有一个拇指,但它设置为透明:

<ControlTemplate x:Key="PhoneSimpleThumb" TargetType="Thumb">
    <Rectangle Fill="Transparent"/>
</ControlTemplate>

上述设置一切正常,如果我点击滑块的角(或部分),则活动区域会朝向该角等等...

现在,如果我想添加缩略图,可以这样说:

<ControlTemplate x:Key="PercentageThumbHorizontal" TargetType="Thumb">
        <Border Margin="-480,-18">
            <Rectangle Width="20" Height="20" RenderTransformOrigin="0.4,0.1" Margin="471,18,470,0" VerticalAlignment="Top" d:LayoutOverrides="Height">
                <Rectangle.Fill>
                    <ImageBrush Stretch="Fill" ImageSource="Resources/DesignElements/SliderThumb.png"/>
                </Rectangle.Fill>
            </Rectangle>
        </Border>
    </ControlTemplate>

滑块失去了它的行为,现在我只能使用拖动事件来改变它的值。现在更具体地说,如果我点击一个滑块部分,活动区域将不会转到该部分,但如果我从一个部分拖动到另一个部分,则滑块活动区域将朝着拖动的方向移动。

这是将使用上述模板的其余代码(我只使用水平滑块)。

    <Style x:Key="PercentageSliderStyle" TargetType="Slider">
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="Maximum" Value="10"/>
        <Setter Property="Minimum" Value="0"/>
        <Setter Property="Value" Value="0"/>
        <Setter Property="Background" Value="{StaticResource PhoneContrastBackgroundBrush}"/>
        <Setter Property="Foreground" Value="{StaticResource PhoneAccentBrush}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Slider">
                    <Grid Background="Transparent">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" To="0.1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="HorizontalTrack"/>
                                        <DoubleAnimation Duration="0" To="0.1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="VerticalTrack"/>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="HorizontalFill">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="VerticalFill">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Grid x:Name="HorizontalTemplate" Margin="{StaticResource PhoneHorizontalMargin}">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="Auto" MinWidth="378"/>
                            </Grid.ColumnDefinitions>
                            <Rectangle x:Name="HorizontalTrack" Grid.ColumnSpan="3" Fill="{TemplateBinding Background}" Height="12" IsHitTestVisible="False" Margin="0,22,0,50" Opacity="0.2"/>
                            <Rectangle x:Name="HorizontalFill" Grid.Column="0" Fill="{TemplateBinding Foreground}" Height="12" IsHitTestVisible="False" Margin="0,22,0,50"/>
                            <RepeatButton x:Name="HorizontalTrackLargeChangeDecreaseRepeatButton" Grid.Column="0" IsTabStop="False" Template="{StaticResource PhoneSimpleRepeatButton}"/>
                            <RepeatButton x:Name="HorizontalTrackLargeChangeIncreaseRepeatButton" Grid.Column="2" IsTabStop="False" Template="{StaticResource PhoneSimpleRepeatButton}"/>
                            <Thumb x:Name="HorizontalThumb"   Width="1" Margin="-1,0,0,0" Grid.Column="1" Template="{StaticResource PercentageThumbHorizontal}" >
                                <!--<Thumb.RenderTransform>
                                    <ScaleTransform ScaleY="1" ScaleX="32"/>
                                </Thumb.RenderTransform> -->            
                            </Thumb>
                        </Grid>
                        <Grid x:Name="VerticalTemplate" Margin="{StaticResource PhoneVerticalMargin}">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="0"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <Rectangle x:Name="VerticalTrack" Fill="{TemplateBinding Background}" IsHitTestVisible="False" Margin="12,0" Opacity="0.2" Grid.RowSpan="3" Width="12"/>
                            <Rectangle x:Name="VerticalFill" Fill="{TemplateBinding Foreground}" IsHitTestVisible="False" Margin="12,0" Grid.Row="2" Width="12"/>
                            <RepeatButton x:Name="VerticalTrackLargeChangeDecreaseRepeatButton" IsTabStop="False" Grid.Row="0" Template="{StaticResource PhoneSimpleRepeatButton}"/>
                            <RepeatButton x:Name="VerticalTrackLargeChangeIncreaseRepeatButton" IsTabStop="False" Grid.Row="2" Template="{StaticResource PhoneSimpleRepeatButton}"/>
                            <Thumb x:Name="VerticalThumb" Grid.Column="1" Margin="-1,0,0,0" RenderTransformOrigin="0.5,0.5" Template="{StaticResource PhoneSimpleThumb}" Width="1">
                                <Thumb.RenderTransform>
                                    <ScaleTransform ScaleY="32" ScaleX="1"/>
                                </Thumb.RenderTransform>
                            </Thumb>
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

你们中有人知道这里发生了什么吗?

谢谢。

【问题讨论】:

    标签: silverlight-3.0 windows-phone-7 slider


    【解决方案1】:

    大的负边距

    <Border Margin="-480,-18">
    

    是我开始寻找的地方。

    先尝试为拇指设置颜色。
    进行小的更改并逐步添加所需的样式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-03
      • 1970-01-01
      相关资源
      最近更新 更多