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

C# 调整wpf窗口大小时如何重新加载工具栏,c#,wpf,C#,Wpf,我有一个工具栏在窗口中,我想隐藏工具栏上的流动按钮 使用以下代码 private void FrameworkElement_OnLoaded(object sender, RoutedEventArgs e) { var toolBar = sender as ToolBar; if (toolBar != null && !toolBar.HasOverflowItems) { var overf

我有一个工具栏在窗口中,我想隐藏工具栏上的流动按钮 使用以下代码

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;
            }
        }
    }
现在,当窗口重新调整大小时,工具栏实际上可以溢出,然后我想再次启动这个加载方法


如何做到这一点?

有一个
SizeChanged
事件,我想这就是您要找的。在窗口的XAML中:

<Window . . .
        SizeChanged="OnSizeChanged">
编辑
更正了第二个代码段以引发加载的事件,而不是SizeChanged事件。

不太了解第二部分,如何引发为工具栏加载的FrameworkElement\u wpf使用路由事件。搜索路由事件以了解其工作方式的详细信息。通过使用我在第二个代码段中包含的代码,您可以通过编程方式让WPF引发事件,就像它正常发生一样。这样,每次发生
SizeChanged
事件时,都会触发
FrameworkElement
class'
Loaded
事件,WPF调用您的处理程序。您的意思是代码段将引发FrameworkElement\u OnLoaded
private void EditorToolBar_OnSizeChanged(对象发送方,SizeChangedEventArgs e){RoutedEventArgs=new RoutedEventArgs(FrameworkElement.LoadedEvent,this);RaiseEvent(args);}
但它不起作用。它不起作用,它从未触发FrameworkElement_OnLoaded事件。然后您在错误的控件上引发加载的事件。您必须在已加载处理程序中包含问题代码的控件上引发事件。假设控件名为
foo
。然后调用
foo.RaiseEvent(args)
RoutedEventArgs args = new RoutedEventArgs( FrameworkElement.LoadedEvent, this );
RaiseEvent(args );