简体   繁体   中英

How to reload the toolbar when the wpf window is resized

I have a tool bar in the window and I want to hide the toolbar over flow button using the following code

private void FrameworkElement_OnLoaded(object sender, RoutedEventArgs e)
    {
        var toolBar = sender as ToolBar;
        if (toolBar != null && !toolBar.HasOverflowItems)
        {
            var overflowGrid = toolBar.Template.FindName("OverflowGrid", toolBar) as FrameworkElement;
            if (overflowGrid != null)
            {
                overflowGrid.Visibility = Visibility.Collapsed;
            }
        }
    }

Now when the window is re-sized, the tool bar can actually over flow then I want to fire this load method again.

How this can be achieved?

There's a SizeChanged event, which I think is what you're looking for. In your window's XAML:

<Window . . .
        SizeChanged="OnSizeChanged">

Then, in your code behind, call your existing function from the OnSizeChanged function.

OR you can raise the FrameworkElement_Loaded event like this:

RoutedEventArgs args = new RoutedEventArgs( FrameworkElement.LoadedEvent, this );
RaiseEvent(args );

EDIT Corrected the second code snippet to raise the Loaded Event, not the SizeChanged event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM