Can't get Thumb from Slider when upgrading to 3.0
Before upgrading I got a Thumb from a Slider like this:
Now it just returns null.
Code: Select all
Loaded += (s, e) =>
{
...
var thumb = (Thumb)uiScaleSlider.GetTemplateChild("SliderThumb");
};
-
-
sfernandez
Site Admin
- Posts: 3363
- Joined:
Re: Can't get Thumb from Slider when upgrading to 3.0
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:
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.
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:
Code: Select all
<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>Hope this helps.
Re: Can't get Thumb from Slider when upgrading to 3.0
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.
Finally, we found that when create view and call IView::SetSize(0, 0) then in Loaded event GetTemplateChild will return null.
-
-
sfernandez
Site Admin
- Posts: 3363
- Joined:
Re: Can't get Thumb from Slider when upgrading to 3.0
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:
Code: Select all
slider->Loaded() += [](BaseComponent* sender, const RoutedEventArgs&)
{
Slider* s = (Slider*)sender;
s->ApplyTemplate();
Thumb* t = (Thumb*)s->GetTemplateChild("Thumb");
//...
};Who is online
Users browsing this forum: No registered users and 5 guests