Find element in WPF scrollbar

After I spend some hours to design a scrollbar in WPF I have a problem. The whole scrollbar is defined in a Resource Dictionary. I create a vertical scrollbar, the XAML code is here

<!-- Vertical ScrollBar with grip control definition -->
  <ControlTemplate x:Key="VerticalScrollBarWithGrip" TargetType="{x:Type ScrollBar}">
    <Grid>
      <Grid.RowDefinitions>
        <RowDefinition MaxHeight="18"/>
        <RowDefinition MaxHeight="18"/>
        <RowDefinition Height="0.00001*"/>
        <RowDefinition MaxHeight="18"/>
      </Grid.RowDefinitions>

      <!-- GripControl -->
      <Border BorderThickness="1" CornerRadius="1" Name="PART_SplittGripCtrl">
        <Border.Style>
          <Style>
            <Setter Property="Border.Background" Value="{StaticResource ScrollBarLightColor}"/>
            <Setter Property="Border.BorderBrush" Value="#C9C9C9"/>
            <Style.Triggers>
              <Trigger Property="Border.IsMouseOver" Value="True">
                <Setter Property="Border.BorderBrush" Value="{StaticResource BorderHighlight}"/>
                <Setter Property="Border.Background" Value="{StaticResource BorderHighlight}"/>
                <Setter Property="Border.Cursor" Value="SizeNS"/>
              </Trigger>
            </Style.Triggers>
          </Style>
        </Border.Style>
        <Image Source="Res/grip.png" Height="17" Width="17" RenderOptions.BitmapScalingMode="NearestNeighbor"/>
      </Border>

      <Border Grid.Row="1" Grid.RowSpan="3" Background="{StaticResource ScrollBarLightColor}"/>
      <RepeatButton Grid.Row="1" Style="{StaticResource ScrollBarLineButton}" Height="18" Command="ScrollBar.LineUpCommand" Content="M 0 5 L 10 5 L 5 0 Z"/>
      <Track x:Name="PART_Track" Grid.Row="2" IsDirectionReversed="true">
        <Track.DecreaseRepeatButton>
          <RepeatButton Style="{StaticResource ScrollBarPageButton}" Command="ScrollBar.PageUpCommand"/>
        </Track.DecreaseRepeatButton>
        <Track.Thumb>
          <Thumb Style="{StaticResource ScrollBarThumb}" Margin="5,2">
            <Thumb.Background>
              <SolidColorBrush Color="{StaticResource DefaultScrollBarColor}"/>
            </Thumb.Background>
          </Thumb>
        </Track.Thumb>
        <Track.IncreaseRepeatButton>
          <RepeatButton Style="{StaticResource ScrollBarPageButton}" Command="ScrollBar.PageDownCommand"/>
        </Track.IncreaseRepeatButton>
      </Track>
      <RepeatButton Grid.Row="4" Style="{StaticResource ScrollBarLineButton}" Height="18" Command="ScrollBar.LineDownCommand" Content="M 0 0 L 5 5 L 10 0 Z"/>
    </Grid>
  </ControlTemplate>

You can see, I added an element named "PART_SplittGripCtrl". The picture in the bottom shows you, how my scrollbar looks like.

Grip in Scrollbar

Yes, my scrollbar should have a similar behaviour to that of Visual Studio.

Now I want to find the "PART_SplittGripCtrl" in C# code, but I don't find this element. So I have some questions.

  1. How can I find PART_SplittGripCtrl in a scrollbar resource?
  2. How can I add events to a WPF element, that is created in a Resource Dictionary?
  3. At the moment I forgot, what I wanted to ask

For some tips I'm very grateful.

***** Edit *****

I rename some parts now. My CustomControl Looks like this

[TemplatePart(Name = "PART_SplittGripCtrl", Type =typeof(Border))]
  public class VsScrollbar : ScrollBar
  {
    static VsScrollbar()
    {
      DefaultStyleKeyProperty.OverrideMetadata(typeof(VsScrollbar), new FrameworkPropertyMetadata(typeof(VsScrollbar)));
    }

    public override void OnApplyTemplate()
    {
      base.OnApplyTemplate();
    }
  }

The Generic.xaml is here

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Org.Vs.ScrollbarTest.CustomControls">


  <Style TargetType="{x:Type local:VsScrollbar}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type local:VsScrollbar}">
          <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
          </Border>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</ResourceDictionary>


ANSWERS:


Apart from there being a typo in your template (SplittGripCtrl vs SplitterGripCtrl) doing this in OnApplyTemplate should be working

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        var border= this.GetTemplateChild("SplittGripCtrl") as Border;
        if (border!= null)
        {
            border.Background = new SolidColorBrush(Colors.Red);
            border.MouseUp += (sender, args) => MessageBox.Show("hi");
        }
    }

You should also make a habbit of declaring essential parts of your templates like this. just for the sake of permitting template overrides.

[TemplatePart(Name = "SplitterGripControl", Type = typeof(Border))]

You only need to create a new user defined element. Change it to inherit from Scrollbar. The control will then consist of your custom code class and declarative syntax contained within themes/generic.xaml Create new element example



 MORE:


 ? Wpf TreeView Multi Level hierarchy
 ? Dependency Property does not work after view model was initialized
 ? Removing ListView header borders in Blend
 ? How to set TabItem CommandBindings when using ItemsSource
 ? VisualTreeHelper.FindElementsInHostCoordinates does not return controls with IsHitTestVisible set up to false
 ? Xaml UWP buttons hide/show not working using INotifyPropertyChanged
 ? Tabbing trough textboxes not working properly
 ? Animation restarts repeatedly when mouse touches the boundaries
 ? Why sub-menu getting opened automatically in WPF MENU?
 ? How to use displaymemberpath on a combobox in UWP C++/CX?