WPF工具栏换行

WPF工具栏换行,wpf,toolbar,word-wrap,Wpf,Toolbar,Word Wrap,我有一个WPF应用程序,它有一个工具栏托盘,由几个工具栏和很多按钮组成。我唯一想做的就是让它们始终可见,无论窗口大小 <ToolBarTray> <ToolBar OverflowMode="Never"> <userControls:SearchUserControl x:Name="SearchControl" /> </ToolBar> <ToolBar ItemsSource="{Binding

我有一个WPF应用程序,它有一个工具栏托盘,由几个工具栏和很多按钮组成。我唯一想做的就是让它们始终可见,无论窗口大小

<ToolBarTray>
    <ToolBar OverflowMode="Never">
        <userControls:SearchUserControl x:Name="SearchControl" />
    </ToolBar>
    <ToolBar ItemsSource="{Binding CommonCommands}" OverflowMode="Never"/>
    <ToolBar ItemsSource="{Binding Type1Commands}" OverflowMode="Never"/>
    <ToolBar ItemsSource="{Binding Type2Commands}" OverflowMode="Never" />
    <ToolBar ItemsSource="{Binding Type3Commands}" OverflowMode="Never" />
</ToolBarTray>

我设置OverflowMode=“Never”以在没有足够空间时去掉按钮右侧的小箭头,因为我总是希望所有按钮都可见。我的“命令”列表是RoutedCommand,带有一个显示为按钮的数据模板,但无论我使用什么,它的反应都是一样的

如果我将工具栏托盘放在StackPanel中,它们的按钮/工具栏将继续超过窗口大小。如果我将工具栏托盘放在包装内,而不是将其包装,则会完全隐藏按钮(只保留工具栏夹点)


我希望实现的功能是,工具栏可以动态更改其标注栏属性,以便在没有可用空间的情况下,工具栏切换到第二个标注栏(行),而不是将按钮隐藏在溢出面板中。

包装工具栏的内容并不容易,但这当然是可能的。关键是标准的
工具栏
控件需要一个名为“PART\u ToolBarPanel”的
TemplatePart
,其类型必须是
ToolBarPanel
。工具栏面板继承自
StackPanel
,但是为了实现目标,您应该使用
WrapPanel
。 因此,由于ToolBarPanel继承自StackPanel,因此它不能同时继承自WrapPanel

我的想法是扩展工具栏面板并将其伪装成一个包裹。首先,让我们看一下工具栏面板的代码(我使用ILSpy来检索WrapPanel代码行为):

现在,我们只需要在XAML中定义几个资源(再次是ILSpy):


现在,让我们创建一个小窗口(300x300)并添加以下XAML:

<ToolBarTray>
    <ToolBar OverflowMode="Never" Band="1">
        <Button Content="Button1" Width="76" Height="26" Margin="2" />
        <Button Content="Button2" Width="76" Height="26" Margin="2" />
        <Button Content="Button3" Width="76" Height="26" Margin="2" />
        <Button Content="Button4" Width="76" Height="26" Margin="2" />
        <Button Content="Button5" Width="76" Height="26" Margin="2" />
        <Button Content="Button6" Width="76" Height="26" Margin="2" />
    </ToolBar>
    <ToolBar Style="{StaticResource NoOverflowItems}" Band="2">
        <Button Content="ButtonA" Width="76" Height="26" Margin="2" />
        <Button Content="ButtonB" Width="76" Height="26" Margin="2" />
        <Button Content="ButtonC" Width="76" Height="26" Margin="2" />
        <Button Content="ButtonD" Width="76" Height="26" Margin="2" />
        <Button Content="ButtonF" Width="76" Height="26" Margin="2" />
        <Button Content="ButtonG" Width="76" Height="26" Margin="2" />
    </ToolBar>
</ToolBarTray>


我希望我的代码能帮助您。

包装工具栏的内容并不容易,但当然是可能的。关键是标准的
工具栏
控件需要一个名为“PART\u ToolBarPanel”的
TemplatePart
,其类型必须是
ToolBarPanel
。工具栏面板继承自
StackPanel
,但是为了实现目标,您应该使用
WrapPanel
。 因此,由于ToolBarPanel继承自StackPanel,因此它不能同时继承自WrapPanel

我的想法是扩展工具栏面板并将其伪装成一个包裹。首先,让我们看一下工具栏面板的代码(我使用ILSpy来检索WrapPanel代码行为):

现在,我们只需要在XAML中定义几个资源(再次是ILSpy):


现在,让我们创建一个小窗口(300x300)并添加以下XAML:

<ToolBarTray>
    <ToolBar OverflowMode="Never" Band="1">
        <Button Content="Button1" Width="76" Height="26" Margin="2" />
        <Button Content="Button2" Width="76" Height="26" Margin="2" />
        <Button Content="Button3" Width="76" Height="26" Margin="2" />
        <Button Content="Button4" Width="76" Height="26" Margin="2" />
        <Button Content="Button5" Width="76" Height="26" Margin="2" />
        <Button Content="Button6" Width="76" Height="26" Margin="2" />
    </ToolBar>
    <ToolBar Style="{StaticResource NoOverflowItems}" Band="2">
        <Button Content="ButtonA" Width="76" Height="26" Margin="2" />
        <Button Content="ButtonB" Width="76" Height="26" Margin="2" />
        <Button Content="ButtonC" Width="76" Height="26" Margin="2" />
        <Button Content="ButtonD" Width="76" Height="26" Margin="2" />
        <Button Content="ButtonF" Width="76" Height="26" Margin="2" />
        <Button Content="ButtonG" Width="76" Height="26" Margin="2" />
    </ToolBar>
</ToolBarTray>

我希望我的代码能帮助您。

4
<Style x:Key="ToolbarThumb" TargetType="{x:Type Thumb}">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Thumb}">
                <Border Padding="{TemplateBinding Control.Padding}" Background="#00FFFFFF" SnapsToDevicePixels="True">
                    <Rectangle>
                        <Rectangle.Fill>
                            <DrawingBrush Viewbox="0,0,4,4" Viewport="0,0,4,4" TileMode="Tile" ViewportUnits="Absolute" ViewboxUnits="Absolute">
                                <DrawingBrush.Drawing>
                                    <DrawingGroup>
                                        <DrawingGroup.Children>
                                            <GeometryDrawing Brush="#FFFFFFFF" Geometry="M1,1L1,3 3,3 3,1z" />
                                            <GeometryDrawing Brush="#A0A0A0" Geometry="M0,0L0,2 2,2 2,0z" />
                                        </DrawingGroup.Children>
                                    </DrawingGroup>
                                </DrawingBrush.Drawing>
                            </DrawingBrush>
                        </Rectangle.Fill>
                    </Rectangle>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="UIElement.IsMouseOver" Value="True">
                        <Setter Property="FrameworkElement.Cursor" Value="SizeAll" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style x:Key="NoOverflowItems" TargetType="ToolBar" BasedOn="{StaticResource {x:Type ToolBar}}">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToolBar}">
                <Grid Name="Grid" Margin="3,1,1,1" SnapsToDevicePixels="True">
                    <Border Name="MainPanelBorder" Background="{TemplateBinding Control.Background}" BorderBrush="{TemplateBinding Control.BorderBrush}" BorderThickness="{TemplateBinding Control.BorderThickness}" Padding="{TemplateBinding Control.Padding}">
                        <DockPanel KeyboardNavigation.TabIndex="1" KeyboardNavigation.TabNavigation="Local">
                            <Thumb Name="ToolBarThumb" Style="{StaticResource ToolbarThumb}" Margin="-3,-1,0,0" Width="10" Padding="6,5,1,6" />
                            <ContentPresenter Name="ToolBarHeader" ContentSource="Header" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="4,0,4,0" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                            <local:ToolBarPanel x:Name="PART_ToolBarPanel" IsItemsHost="True" Margin="0,1,2,2" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                        </DockPanel>
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Value="{x:Null}" Property="HeaderedItemsControl.Header">
                        <Setter TargetName="ToolBarHeader" Property="UIElement.Visibility" Value="Collapsed" />
                    </Trigger>
                    <Trigger Property="ToolBarTray.IsLocked" Value="True">
                        <Setter TargetName="ToolBarThumb" Property="UIElement.Visibility" Value="Collapsed" />
                    </Trigger>
                    <Trigger Property="ToolBar.Orientation" Value="Vertical">
                        <Setter TargetName="Grid" Property="FrameworkElement.Margin" Value="1,3,1,1" />
                        <Setter TargetName="ToolBarThumb" Property="FrameworkElement.Height" Value="10" />
                        <Setter TargetName="ToolBarThumb" Property="FrameworkElement.Width" Value="Auto" />
                        <Setter TargetName="ToolBarThumb" Property="FrameworkElement.Margin" Value="-1,-3,0,0" />
                        <Setter TargetName="ToolBarThumb" Property="Control.Padding" Value="5,6,6,1" />
                        <Setter TargetName="ToolBarHeader" Property="FrameworkElement.Margin" Value="0,0,0,4" />
                        <Setter TargetName="PART_ToolBarPanel" Property="FrameworkElement.Margin" Value="1,0,2,2" />
                        <Setter TargetName="ToolBarThumb" Property="DockPanel.Dock" Value="Top" />
                        <Setter TargetName="ToolBarHeader" Property="DockPanel.Dock" Value="Top" />

                        <Setter TargetName="MainPanelBorder" Property="FrameworkElement.Margin" Value="0,0,0,11" />
                    </Trigger>
                    <Trigger Property="UIElement.IsEnabled" Value="False">
                        <Setter Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" Property="Control.Foreground" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<ToolBarTray>
    <ToolBar OverflowMode="Never" Band="1">
        <Button Content="Button1" Width="76" Height="26" Margin="2" />
        <Button Content="Button2" Width="76" Height="26" Margin="2" />
        <Button Content="Button3" Width="76" Height="26" Margin="2" />
        <Button Content="Button4" Width="76" Height="26" Margin="2" />
        <Button Content="Button5" Width="76" Height="26" Margin="2" />
        <Button Content="Button6" Width="76" Height="26" Margin="2" />
    </ToolBar>
    <ToolBar Style="{StaticResource NoOverflowItems}" Band="2">
        <Button Content="ButtonA" Width="76" Height="26" Margin="2" />
        <Button Content="ButtonB" Width="76" Height="26" Margin="2" />
        <Button Content="ButtonC" Width="76" Height="26" Margin="2" />
        <Button Content="ButtonD" Width="76" Height="26" Margin="2" />
        <Button Content="ButtonF" Width="76" Height="26" Margin="2" />
        <Button Content="ButtonG" Width="76" Height="26" Margin="2" />
    </ToolBar>
</ToolBarTray>