简体   繁体   English

调整wpf窗口大小时如何重新加载工具栏

[英]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. 有一个SizeChanged事件,我想这就是您要寻找的事件。 In your window's XAML: 在窗口的XAML中:

<Window . . .
        SizeChanged="OnSizeChanged">

Then, in your code behind, call your existing function from the OnSizeChanged function. 然后,在后面的代码中,从OnSizeChanged函数调用现有函数。

OR you can raise the FrameworkElement_Loaded event like this: 或者,您可以像这样引发FrameworkElement_Loaded事件:

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

EDIT Corrected the second code snippet to raise the Loaded Event, not the SizeChanged event. EDIT更正了第二个代码片段,以引发Loaded事件,而不是SizeChanged事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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