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 );

            }

        }

 

转载于:https://www.cnblogs.com/GeeWu/archive/2011/05/19/wpf_toolbar.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值