Search code examples
c#uwpframeworkelement

How to run UWP FrameworkElement SizeChanged method on Visiblity.Collapsed?


I want to show FrameworkElement when its width>50 else collapse it

private void PB_LocalDrive_SizeChanged(object sender, SizeChangedEventArgs e)
{
    var progressBar = sender as FrameworkElement;
    progressBar.Visibility = progressBar.ActualWidth > 50 ? Visibility.Visible : Visibility.Collapsed;
}

but this works only when it is visible (and not collapsed). How can I run this method even if its collapsed?

I also tried access it from its parent but result is same. Element is not accessible after collapsed

private void Parent_SizeChanged(object sender, SizeChangedEventArgs e)
{
    var stackPanel = sender as FrameworkElement;
    var progressBar = (ProgressBar)stackPanel.FindName("PB_LocalDrive");
    progressBar.Visibility = progressBar.ActualWidth > 50 ? Visibility.Visible : Visibility.Collapsed;
}

Solution

  • This very practice that can lead to immensely complicated issues, this needs to be highlighted as the other two users fail to notice it.

    Never change the visibility or other Layout States of an element based of its parent Current Size.

    This is because an infinite UI loop can be created as the contained items of a said Parent control can effect the parent in a two way fashion causing the SizeChanged event firing constantly, it requires me lots of examples for me to showcase this nicely but I think its better to present you with a bunch of widely used and accepted solutions for your particular issue.

    First things first, dont use a the FindName, if you have a name on your control just directly reference it, no need for the added overhead of the function.

    Secondly use an AdaptiveTrigger, it is a xaml first approach that is cleaner and more nice than the code approach, for more info check this out: https://learn.microsoft.com/en-us/uwp/api/Windows.UI.Xaml.AdaptiveTrigger

    Lastly, if you really want to stick with the code approach, then make sure to compare against something more Solid and defined in terms of Sizes, and this is the Current window of your app, it will never overflow outside the Application visible space and it will never change no matter what happens inside its contents, it comes in a nice location over at Window.Current.Bounds