-
- Mikhail Yanakayev
- Posts: 15
- Joined:
How to properly get element inside ControlTemplate in Native code
Hello,
In XAML I have construction like this:
Then I'm trying to get this element by following code:
Here "component" receives nullptr. Also I've tried to call ApplyTemplate before getting child and also make child class with OnApplyTemplate callback that never called.
So how I can get ScrollViewer element from this template?
Also is it possible than scroll this ScrollViewer to needed row considering that I'm using virtualization for it?
Thank you!
In XAML I have construction like this:
Code: Select all
<Grid Grid.Row="1" Grid.Column="1">
<ItemsControl x:Name="MyGrid" ItemsSource="{Binding MyList}" ItemTemplate="{DynamicResource MyRow}" Grid.Row="1" Grid.Column="1" VirtualizingPanel.ScrollUnit="Pixel" VirtualizingPanel.CacheLengthUnit="Item" >
<ItemsControl.Template>
<ControlTemplate>
<ScrollViewer x:Name="MyScroll" CanContentScroll="True" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Visible" Template="{StaticResource Template.ScrollViewer}" PanningMode="VerticalOnly" Focusable="False" >
<VirtualizingStackPanel IsItemsHost="True" />
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
</ItemsControl>
</Grid>
Code: Select all
Noesis::ItemsControl* myGridControl = uiControl->FindName<Noesis::ItemsControl>("MyGrid");
//m_myGridControl->ApplyTemplate();
Noesis::BaseComponent* component = myGridControl->GetTemplateChild("MyScroll");
So how I can get ScrollViewer element from this template?
Also is it possible than scroll this ScrollViewer to needed row considering that I'm using virtualization for it?
Thank you!
-
-
sfernandez
Site Admin
- Posts: 3363
- Joined:
Re: How to properly get element inside ControlTemplate in Native code
Hi,
When are you executing this code?
You should wait until the control gets Initialized (you'll need to manually call ApplyTemplate in this case) or Loaded (template will be already applied).
You can hook to those events when your user control is created:
Would that work for you?
When are you executing this code?
You should wait until the control gets Initialized (you'll need to manually call ApplyTemplate in this case) or Loaded (template will be already applied).
You can hook to those events when your user control is created:
Code: Select all
MyControl()
{
InitializeComponent();
Loaded() += [](BaseComponent* sender, const RoutedEventArgs&)
{
FrameworkElement* root = (FrameworkElement*)sender;
ItemsControl* ic = root->FindName<ItemsControl>("MyGrid");
BaseComponent* sv = ic->GetTemplateChild<ScrollViewer>("MyScroll");
...
};
}-
- Mikhail Yanakayev
- Posts: 15
- Joined:
Re: How to properly get element inside ControlTemplate in Native code
Yeah, seems it works.
What about second question? I've tried to call ScrollViewer::ScrollToBottom for virtualized ScrollView, and it scrolled not to bottom but seems to bottom part of current chunk that loaded at the moment. Is it possible to scroll it to the real end (or to exact place) of such ScrollView?
What about second question? I've tried to call ScrollViewer::ScrollToBottom for virtualized ScrollView, and it scrolled not to bottom but seems to bottom part of current chunk that loaded at the moment. Is it possible to scroll it to the real end (or to exact place) of such ScrollView?
-
-
sfernandez
Site Admin
- Posts: 3363
- Joined:
Re: How to properly get element inside ControlTemplate in Native code
In order to scroll in a virtualized panel, as the containers are probably not visible in the ScrollViewer, the panel needs to generate the containers for the new position. So you have to indicate that to the virtualized panel:
Could you try something like that?
Code: Select all
void ScrollToItem(VirtualizingPanel* panel, int index)
{
panel->BringIndexIntoView(index);
}-
- Mikhail Yanakayev
- Posts: 15
- Joined:
Re: How to properly get element inside ControlTemplate in Native code
Yes, it works.
For now I can't do this right after adding all elements to the scrollviewer (seems they're not generated at the moment), but it's another story.
Thanks!
For now I can't do this right after adding all elements to the scrollviewer (seems they're not generated at the moment), but it's another story.
Thanks!
-
-
sfernandez
Site Admin
- Posts: 3363
- Joined:
Who is online
Users browsing this forum: Bing [Bot] and 5 guests