When does RadCarousel create its RadCarouselPanel?

1 Answer 51 Views
Carousel
Joe
Top achievements
Rank 2
Iron
Iron
Veteran
Joe asked on 14 Apr 2023, 07:06 PM | edited on 14 Apr 2023, 07:11 PM
I am attempting to get a reference to the RadCarouselPanel in my RadCarousel.  I need to know when it is animating.  But unless I wait for some sort of manual event (like a keyboard press)  the panel is null.

First I tried adding a handler for the Carousel's Loaded event 


<tk:RadCarousel x:Name="OtherScanGallery"

    ItemsSource="{Binding OtherScansView}"
    VerticalContentAlignment="Top"
    IsSynchronizedWithCurrentItem="True"
    IsVisibleChanged="OtherScanGallery_OnIsVisibleChanged"
    SelectionChanged="OtherScanGallery_OnSelectionChanged"
    Loaded="OtherScanGallery_OnLoaded"
    SelectedItem="{Binding ElementName=Root, Path=DataContext.SelectedOtherScan, Mode=TwoWay}"
    >

private void OtherScanGallery_OnLoaded(object sender, RoutedEventArgs e) {

RadCarouselPanel? panel = null;

if (sender is not RadCarousel car) Log.Debug($"Gallery loaded but no carousel"); elseif (null == (panel = car.FindCarouselPanel())) Log.Debug($"Gallery loaded but no panel"); else Log.Debug($"Gallery fully loaded"); }


Unfortunately the second check fails here.  FindCarouselPanel returns null.    I guess this makes some sense since the carousel is initially not visible when my page is loaded.    So I figured, maybe the carousel waits until it becomes visible to create it's panel

So then I tried to put in a handler for the carousel's IsVisibleChanged event.  I figure the carousel will have to have created the panel by then, right? 

private void OtherScanGallery_OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    var panel = OtherScanGallery.FindCarouselPanel();
    if (null == panel )
        Log.Error("No panel after visible changed.");
}


But no, wrong.  The Panel returned here is still null

However If i then set up an event handler for... say... a keyboard event later on, at that point FindCarouselPanel returns a valid panel.

So what are the rules here?  How do I determine definitively when the Carousel creates its panel?


Note:  I am using The Windows8Touch theme in the binaries with XAML and the StyleManager.  I have not created an sort of ControlTemplate for the carousel or its items.  I am using the theme-provided ones



1 Answer, 1 is accepted

Sort by
0
Accepted
Martin Ivanov
Telerik team
answered on 19 Apr 2023, 09:00 AM

Hello Joe,

The FindCarouselPanel method relies on that the visual tree of the RadCarousel control is loaded. When the Visibility is set to Collapsed initially, the visual tree doesn't exist, therefore the FindCarouselPanel method will return null value.

To achieve your requirement, the carousel visual tree should be loaded. WPF doesn't have a proper event that notifies about this action, so the approach that you should use will depend on the exact scenario. 

In your case you can use the IsVisibleChanged event, but you are going to need to wait a bit before getting the template. That is because the IsVisibleChanged event fires as soon as the IsVisible property of the control changes. In other words, the event doesn't wait for the control to get displayed. To wait for that, you can use the Dispatcher object which will delay the code execution a bit. 

private void OtherScanGallery_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
	Dispatcher.BeginInvoke(new Action(() =>
	{
		var panel = this.OtherScanGallery.FindChildByType<RadCarouselPanel>();
	}),(DispatcherPriority)3);
}

Regards,
Martin Ivanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Joe
Top achievements
Rank 2
Iron
Iron
Veteran
commented on 04 May 2023, 06:37 PM

That did the trick, thank you!
Tags
Carousel
Asked by
Joe
Top achievements
Rank 2
Iron
Iron
Veteran
Answers by
Martin Ivanov
Telerik team
Share this question
or