3

我正在编写一个自定义滚动条。当鼠标悬停在网格(滚动条)上时,它应该增加它的高度和拇指的高度并改变背景等。除了拇指的高度之外,一切都完成了,因为拇指是网格的子元素而不是它的属性之一。

这是完整的模板代码,它给了我找不到 TargetName“ellipse”的异常。

<ControlTemplate x:Key="HorizontalScrollBar" TargetType="{x:Type ScrollBar}">
    <Grid MaxHeight="28">
        <Grid.ColumnDefinitions>
            <ColumnDefinition MaxWidth="2"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition MaxWidth="2"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Track Name="PART_Track" Grid.Column="1" IsDirectionReversed="False">
            <Track.DecreaseRepeatButton>
                <RepeatButton Command="ScrollBar.PageLeftCommand"
                    Style="{StaticResource ScrollBarPageButtonStyle}">
                </RepeatButton>
            </Track.DecreaseRepeatButton>
            <Track.Thumb>
                <Thumb IsTabStop="False" Margin="0,1,0,1" BorderBrush="{StaticResource StandardBorderBrush}">
                    <Thumb.Template>
                        <ControlTemplate TargetType="{x:Type Thumb}">
                            <Rectangle Height="12" Name="ellipse"  Stroke="{StaticResource StandardBorderBrush}"
                                            Fill="{StaticResource StandardBrush}" RadiusX="5" RadiusY="5"></Rectangle>
                        </ControlTemplate>
                    </Thumb.Template>                        
                </Thumb>                    
            </Track.Thumb>
            <Track.IncreaseRepeatButton>
                <RepeatButton Command="ScrollBar.PageRightCommand"
                    Style="{StaticResource ScrollBarPageButtonStyle}">
                </RepeatButton>
            </Track.IncreaseRepeatButton>
        </Track>
        <Grid.Resources>
            <Style TargetType="Grid">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="Gray"></Setter>
                        <Setter TargetName="ellipse" Property="Height" Value="28"></Setter>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="False">
                        <Setter Property="Background" Value="Transparent"></Setter>
                        <Setter TargetName="ellipse" Property="Height" Value="14"></Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Grid.Resources>
    </Grid>
</ControlTemplate>
4

1 回答 1

3

用于ControlTemplate.Triggers您的拇指模板,例如

  <Track.Thumb>
     <Thumb IsTabStop="False" Margin="0,1,0,1" BorderBrush="Blue">
        <Thumb.Template>
           <ControlTemplate TargetType="{x:Type Thumb}">
              <Rectangle Height="12" x:Name="ellipse"  Stroke="Blue"
                                        Fill="Wheat" RadiusX="5" RadiusY="5"></Rectangle>
                   <ControlTemplate.Triggers>
                        <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}}"  Value="True">
                             <Setter TargetName="ellipse" Property="Height" Value="28"></Setter>
                          </DataTrigger>
                          <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}}"  Value="True">
                                <Setter TargetName="ellipse" Property="Height" Value="14"></Setter>
                           </DataTrigger>
                    </ControlTemplate.Triggers>
                 </ControlTemplate>
               </Thumb.Template>
           </Thumb>
   </Track.Thumb>

并且您的样式触发器将被更新

 <Style TargetType="Grid">
     <Style.Triggers>
          <Trigger Property="IsMouseOver" Value="True">
              <Setter Property="Background" Value="Gray"></Setter>
          </Trigger>
          <Trigger Property="IsMouseOver" Value="False">
                <Setter Property="Background" Value="Transparent"></Setter>
          </Trigger>
       </Style.Triggers>
   </Style>
于 2014-06-05T16:23:58.830 回答