XamarinCommunityToolkit
XamarinCommunityToolkit copied to clipboard
[Bug] [Expander] Invoke method ForceUpdateSize on Content MeasureInvalidated
Seems like expander is caching the last size of content and expanding to that size. But there is no change when content size changed if add some views to layouts like StackLayout, Grid inside an expander
Description
Need to clear cache of last content holder size and force measure of the content on it's measure change
Steps to Reproduce
- Create expander with content like grid
- Expand the expander
- Add views to grid
Expected Behavior
Change the size of expander on content size change
Actual Behavior
Size of expander remains the same as last expansion
Basic Information
- Version with issue: 1.3.1
- Last known good version: None
Workaround
Invoke ForceUpdateSize on expander when its content size change
@FrozDark Could you attach a small repro?
@pictos Yep ExpanderSizeIssueRepro.zip
@FrozDark hi Yes, this is the limitation of Expander
You should call ForceUpdateSize
or ForceUpdateSizeCommand
on your own after any content changes.
For others still experiencing this, the easiest way to do this through MVVM is via the MessagingCenter.
In your View Model:
void EnqueueForceResizeExpanders() => MessagingCenter.Send(this, "ExpanderForceResize");
In your View code-behind (.xaml.cs
):
private void ForceResizeExpanders<T>(T _)
{
foreach (View childView in MyLayoutOfExpanders.Children)
{
if (childView is Expander expander && expander.IsExpanded)
{
expander.ForceUpdateSize();
}
}
}
The method is generic and the sender parameter is discarded because it is irrelevant for this messaging scenario.
In OnAppearing()
:
MessagingCenter.Subscribe<MyViewModel>(this, "ExpanderForceResize", ForceResizeExpanders);
In OnDisappearing()
(clean up to prevent leaking memory through orphaned event subscriptions):
MessagingCenter.Unsubscribe<MyViewModel>(this, "ExpanderForceResize");
If you have only a few Expanders and they are static (not templated as part of a collection) then you can simplify the foreach
loop and reference them each directly but the principle remains.
MyLayoutOfExpanders
What does MyLayoutOfExpanders represents in a CollectionView with an ItemTemplate @ahoke-cr ?
@gsgou it is any layout view (like a CollectionView) that has children of type Expander (which could come from a DataTemplate).
@ahoke-cr tks for the quick response. While a Layout has a Children property a CollectionView does not. It does have though ChildAdded() so could be used with a behaviour.