• WPF控件经验小结:(1) ToolBar去掉右边箭头(扩展图标)



    今天开发时,同事问我一个问题。怎么去除ToolBar右边扩展图标。我想了一下,说改Style。同事说太麻烦了。可不可以快速修正。我说应该动态去读取Template模板中的Style,然后隐藏。怎么实现那?

    第一步,分析现有的ToolBar的样式(Style)

    <Grid x:Name="Grid" Margin="3,1,1,1" SnapsToDevicePixels="true">
                            
    <Grid x:Name="OverflowGrid" HorizontalAlignment="Right" Visibility="Hidden">
                                
    <ToggleButton x:Name="OverflowButton" ClickMode="Press" FocusVisualStyle="{x:Null}" IsChecked="{Binding IsOverflowOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" IsEnabled="{TemplateBinding HasOverflowItems}" Style="{StaticResource ToolBarHorizontalOverflowButtonStyle}"/>
                                
    <Popup x:Name="OverflowPopup" AllowsTransparency="true" Focusable="false" IsOpen="{Binding IsOverflowOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom" StaysOpen="false">
                                    
    <Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent">
                                        
    <Border x:Name="ToolBarSubMenuBorder" BorderBrush="{StaticResource ToolBarMenuBorder}" BorderThickness="1" Background="{StaticResource ToolBarSubMenuBackground}" RenderOptions.ClearTypeHint="Enabled">
                                            
    <ToolBarOverflowPanel x:Name="PART_ToolBarOverflowPanel" KeyboardNavigation.DirectionalNavigation="Cycle" FocusVisualStyle="{x:Null}" Focusable="true" Margin="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.TabNavigation="Cycle" WrapWidth="200"/>
                                        
    </Border>
                                    
    </Microsoft_Windows_Themes:SystemDropShadowChrome>
                                
    </Popup>
                            
    </Grid>
                            
    <Border x:Name="MainPanelBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" Style="{StaticResource ToolBarMainPanelBorderStyle}" Margin="0">
                                
    <DockPanel KeyboardNavigation.TabIndex="1" KeyboardNavigation.TabNavigation="Local">
                                    
    <Thumb x:Name="ToolBarThumb" Margin="-3,-1,0,0" Padding="6,5,1,6" Style="{StaticResource ToolBarThumbStyle}" Width="10"/>
                                    
    <ContentPresenter x:Name="ToolBarHeader" ContentSource="Header" HorizontalAlignment="Center" Margin="4,0,4,0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/>
                                    
    <ToolBarPanel x:Name="PART_ToolBarPanel" IsItemsHost="true" Margin="0,1,2,2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                
    </DockPanel>
                            
    </Border>
                        
    </Grid>

    2,找到对应的控件,隐藏 OverflowGridMainPanelBorder


    Toolbar bar = new Toolbar();

    Bar.Loaded 
    += new EventHandler();

     


    private void ToolBar_Loaded(object sender, RoutedEventArgs e)

            {

                ToolBar toolBar 
    = sender as ToolBar;

                var overflowGrid 
    = toolBar.Template.FindName("OverflowGrid", toolBar) as FrameworkElement;

                
    if (overflowGrid != null)

                {

                    overflowGrid.Visibility 
    = Visibility.Collapsed;

                }

     

                var mainPanelBorder 
    = toolBar.Template.FindName("MainPanelBorder", toolBar) as FrameworkElement;

               
    if (mainPanelBorder != null)

                {

                    mainPanelBorder.Margin 
    = new Thickness(0);

                }

            }

     

  • 相关阅读:
    Ubuntu16.04 LTS 安装VNC Server开启远程桌面
    duilib List右键菜单的响应
    centos bootloader安装到u盘后修复方法
    dpkg: error processing package XXX (--configure) 解决方法 (ubuntu右上角红色警告)
    linux centos ftp服务器搭建
    windows 端口转发 netsh
    遍历windows窗口
    解除文件占用
    VS 运行库MT、MD的区别
    点击显示桌面的窗口状态
  • 原文地址:https://www.cnblogs.com/GeeWu/p/wpf_toolbar.html
Copyright © 2020-2023  润新知