ExpanderView expands after page navigation

I use the WPF ExpanderView port ( ExpanderRT ) in my UWP application to display extensible headers with elements. This works great when the application launches for the first time and is initialized by MainPage . However, if you go to a new page and then return to MainPage , then ExpanderView will look advanced, but not display elements. It should look the same as when MainPage was first initialized. I displayed a GIF to show the behavior.

This is a XAML UserControl on MainPage ;

 <ListView x:Name="CategoriesListView" ScrollViewer.VerticalScrollBarVisibility="Hidden"> <ListView.ItemContainerStyle> <Style TargetType="ListViewItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListViewItem"> <ListViewItemPresenter ContentMargin="0" /> </ControlTemplate> </Setter.Value> </Setter> </Style> </ListView.ItemContainerStyle> <ListView.ItemsPanel> <ItemsPanelTemplate> <StackPanel/> </ItemsPanelTemplate> </ListView.ItemsPanel> <ListView.ItemTemplate> <DataTemplate x:DataType="models:Category"> <expander:ExpanderControl x:Name="expander_Main" ItemsSource="{x:Bind childItems}" Expanded="Expanded" /> </DataTemplate> </ListView.ItemTemplate> 

This is the EventHandler that I use for navigation;

  private void OnSettingsButtonChecked(object sender, RoutedEventArgs e) { ShellSplitView.IsPaneOpen = false; ViewModel.NavigationService.Navigate(typeof(SettingsPage)); } 

If any of you know why ExpanderView has such weird behavior, please give me a hint - I can provide more of my code if you need to.

Capture

[UPDATE]

I noticed that this behavior only occurs when the application is launched on a mobile device (Smartphone or Mobile Windows 10 Emulator). If I run the application on the local computer, ExpanderView working fine. When I use the back button to go to MainPage , it works as expected - I don’t know how to fix it, it’s really strange.

Capture on desktop

+4
source share
1 answer

After debugging all the code of the ExpanderRT control, I was able to solve the problem! This is caused by an error in the original Expander control that was ported to WinRT

I found out that the height of _itemsCanvas in the ExpanderControl.cs class is set using this method:

 private void OnPresenterSizeChanged(object sender, SizeChangedEventArgs e) { if (null != _itemsCanvas && null != _presenter && IsExpanded) { _itemsCanvas.Height = _presenter.DesiredSize.Height; } } 

This causes the element container size to be applied only when ExpanderView is currently expanding. I simply added the following condition to set the height of the element container to 0 when the ExpanderView is not currently expanding.

 else if (null != _itemsCanvas && null != _presenter && !IsExpanded) { _itemsCanvas.Height = 0; } 

I have added various improvements to the control to take advantage of the new features of UWP, so if anyone is interested in a version compatible with UW, feel free to contact me.

UPDATE

I created a GitHub Repo for the updated ExpanderControl for UWP

+3
source

Source: https://habr.com/ru/post/1242060/


All Articles