Page 1 of 1

Can't get Thumb from Slider when upgrading to 3.0

Posted: 29 May 2020, 11:44
by asusralis
Before upgrading I got a Thumb from a Slider like this:
Loaded += (s, e) =>
            {
                ...
                var thumb = (Thumb)uiScaleSlider.GetTemplateChild("SliderThumb");
            };
Now it just returns null.

Re: Can't get Thumb from Slider when upgrading to 3.0

Posted: 29 May 2020, 12:03
by sfernandez
Are you using our new theme in 3.0 for your controls or have you created your own?

GetTemplateChild method looks for a named element in the template's visual tree, so depending on what ControlTemplate you are using in your Slider it would return a valid element or not.

For example, the Slider's template in our theme looks like this:
<ControlTemplate x:Key="Template.Slider.Horizontal" TargetType="Slider">
    <Grid x:Name="TemplateRoot" Height="20" Margin="8,0">
      ...
      <Track x:Name="PART_Track" Grid.RowSpan="3" Orientation="Horizontal">
        ...
        <Track.Thumb>
          <Thumb x:Name="Thumb" Background="{TemplateBinding Foreground}" Style="{StaticResource Style.Slider.Thumb}"/>
        </Track.Thumb>
      </Track>
    </Grid>
    ...
</ControlTemplate>
As you can see the Thumb element is named "Thumb" there, but it could also have no name or have any other name, there are no constraints for that. If your code looks for a named element in a template you should be in charge of defining that template or know how it is defined.

Hope this helps.

Re: Can't get Thumb from Slider when upgrading to 3.0

Posted: 13 Apr 2023, 14:52
by decai
We have the same problem after upgrade from 3.1.4 to 3.2.0. GetTemplateChild return null in Loaded event, in LayoutUpdated event GetTemplateChild will return the proper control.

Finally, we found that when create view and call IView::SetSize(0, 0) then in Loaded event GetTemplateChild will return null.

Re: Can't get Thumb from Slider when upgrading to 3.0

Posted: 19 Apr 2023, 12:23
by sfernandez
In any case, if you want to get an element from a control template you can always ensure the generation of the template by calling ApplyTemplate:
slider->Loaded() += [](BaseComponent* sender, const RoutedEventArgs&)
{
  Slider* s = (Slider*)sender;
  s->ApplyTemplate();
  Thumb* t = (Thumb*)s->GetTemplateChild("Thumb");
  //...
};