WPF自定义ToolBar - xaml方式

@hanq  2020年02月26日 11:44

因从网上找到的各种Tool_load方式可能存在问题,故而换为xaml方式:


    <Style x:Key="ToolBarThumbStyle"
       TargetType="{x:Type Thumb}">
            <Setter Property="OverridesDefaultStyle"
          Value="true" />
            <Setter Property="Cursor"
          Value="SizeAll" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Thumb}">
                        <Border Background="Transparent"
                SnapsToDevicePixels="True">
                            <Rectangle Margin="0,2">
                                <Rectangle.Fill>
                                    <DrawingBrush Viewport="0,0,4,4"
                            ViewportUnits="Absolute"
                            Viewbox="0,0,8,8"
                            ViewboxUnits="Absolute"
                            TileMode="Tile">
                                        <DrawingBrush.Drawing>
                                            <DrawingGroup>
                                                <GeometryDrawing Brush="#AAA"
                                     Geometry="M 4 4 L 4 8 L 8 8 L 8 4 z" />
                                            </DrawingGroup>
                                        </DrawingBrush.Drawing>
                                    </DrawingBrush>
                                </Rectangle.Fill>
                            </Rectangle>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="ToolBarStyle" TargetType="{x:Type ToolBar}">
            <Setter Property="OverridesDefaultStyle" Value="True" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToolBar}">
                        <Border CornerRadius="2" BorderThickness="1" Background="Transparent" BorderBrush="Transparent">
                            <DockPanel>
                                <Thumb Style="{StaticResource ToolBarThumbStyle}" Width="5" />
                                <ToolBarPanel IsItemsHost="true" Margin="0,1,1,0"/>
                            </DockPanel>
                        </Border>
                        <ControlTemplate.Triggers>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            ...
        </Style>

需求是,要拖动不要三角菜单。如果不要拖动可以


    <ToolBarTray IsLocked="True" ... />

参考资料:https://docs.microsoft.com/en-us/dotnet/framework/wpf/controls/toolbar-styles-and-templates?redirectedfrom=MSDN


添加新评论