🏆 作者简介,愚公搬代码 🏆《头衔》:华为云特约编辑,华为云云享专家,华为开发者专家,华为产品云测专家,CSDN博客专家,阿里云专家博主,腾讯云优秀博主,掘金优秀博主,51CTO博客专家等。 🏆《近期荣誉》:2022年CSDN博客之星TOP2,2022年华为云十佳博主等。 🏆《博客内容》:.NET、Java、Python、Go、Node、前端、IOS、Android、鸿蒙、Linux、物联网、网络安全、大数据、人工智能、U3D游戏、小程序等相关领域知识。 🏆🎉欢迎 👍点赞✍评论⭐收藏

(文章目录)


🚀前言

WPF控件是Windows Presentation Foundation(WPF)中的基本用户界面元素。它们是可视化对象,可以用来创建各种用户界面。WPF控件可以分为两类:原生控件和自定义控件。

原生控件是由Microsoft提供的内置控件,如Button、TextBox、Label、ComboBox等。这些控件都是WPF中常见的标准用户界面元素。

自定义控件则允许开发人员使用XAML和C#等编程语言来创建个性化的用户界面元素。自定义控件可以根据需求提供更多的功能和自定义化选项,以及更好的用户体验。

🚀一、ToolBarTray控件详解

ToolBarTray控件是WPF中的一个容器控件,用于将多个工具栏(ToolBar)控件组合在一起,并在窗体中显示它们。它类似于Windows应用程序中的工具栏。

ToolBarTray控件的使用方法与其他WPF容器控件类似。您可以在ToolBarTray控件中添加多个ToolBar控件,并对它们进行布局和定位。

下面是ToolBarTray控件的示例代码:

<ToolBarTray>
    <ToolBar>
        <Button Content="Open" />
        <Button Content="Save" />
        <Button Content="Print" />
    </ToolBar>
    <ToolBar>
        <Button Content="Cut" />
        <Button Content="Copy" />
        <Button Content="Paste" />
    </ToolBar>
</ToolBarTray>

在上面的示例中,我们在ToolBarTray控件中定义了两个ToolBar控件,每个ToolBar控件中都包含了若干个Button控件。

注意:ToolBarTray控件中的ToolBar控件是按照水平方向排列的。如果需要垂直排列,可以将ToolBarTray的Orientation属性设置为Vertical即可。

🔎1.属性介绍

WPF中ToolBarTray控件的属性如下:

  1. Background:用于设置ToolBarTray控件的背景色。

  2. Orientation:用于设置ToolBarTray控件中工具栏的放置方向。可以设置为Horizontal或Vertical,默认为Horizontal。

  3. IsLocked:用于控制ToolBarTray中的工具栏是否可以被拖动。可以设置为True或False,默认为False。

  4. ToolBars:用于指定ToolBarTray中包含的工具栏集合。可以在XAML中使用ToolBar组件定义工具栏,并将其添加到ToolBarTray的ToolBars集合中。

  5. OverridesDefaultStyle:用于指定是否忽略ToolBarTray的默认样式。可以设置为True或False,默认为False。

  6. SnapsToDevicePixels:用于指定是否将ToolBarTray对齐到设备像素边界。可以设置为True或False,默认为True。

  7. UseLayoutRounding:用于指定是否对ToolBarTray中的内容使用布局舍入。可以设置为True或False,默认为False。

  8. VerticalAlignment:用于设置ToolBarTray控件的垂直对齐方式。可以设置为Top、Center、Bottom或Stretch,默认为Stretch。

  9. HorizontalAlignment:用于设置ToolBarTray控件的水平对齐方式。可以设置为Left、Center、Right或Stretch,默认为Stretch。

  10. Visibility:用于设置ToolBarTray控件的可见性。可以设置为Visible、Hidden、Collapsed,默认为Visible。

🔎2.常用场景

ToolBarTray控件通常用于WPF桌面应用程序中的工具栏和导航栏。以下是一些常见的使用场景:

  1. 工具栏:ToolBarTray控件可以用于在主窗口中创建工具栏,用于快速访问应用程序中的常用工具和操作。

  2. 导航栏:ToolBarTray控件可以用于创建应用程序中的导航栏,使用户可以轻松地浏览和导航到不同的页面或功能。

  3. 多文档界面:ToolBarTray控件可以与TabControl控件或其他多文档界面控件一起使用,以创建一个具有多个标签页的应用程序界面,并在每个标签页上显示不同的工具栏。

  4. 自定义工具栏:ToolBarTray控件可以用于创建自定义工具栏,例如,根据用户角色或权限设置不同的工具栏。

ToolBarTray控件是一个非常灵活的控件,可以满足许多不同的WPF应用程序中的工具栏和导航栏的需求。

🔎3.具体案例

<Window x:Class="WpfAppTest.ToolBarTrayWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfAppTest"
        mc:Ignorable="d"
        Title="ToolBarTrayWindow" Height="450" Width="800">
        <Grid>
                <ToolBarTray Orientation="Horizontal" Background="LightCyan" IsLocked="False">
                        <ToolBar Band="0" BandIndex="1" HorizontalAlignment="Left" Height="21" VerticalAlignment="Top"  IsOverflowOpen="True" Margin="0.2,0,-19.4,0" Width="100" >
                                <RadioButton Name="rbRed" ToolTip="Red">
                                        <RadioButton.Content>
                                                <Rectangle Width="10" Height="10" Fill="Red"/>
                                        </RadioButton.Content>
                                </RadioButton>
                                <RadioButton  ToolTip="Yellow" >
                                        <RadioButton.Content>
                                                <Rectangle Width="10" Height="10" Fill="Yellow"/>
                                        </RadioButton.Content>
                                </RadioButton>
                                <RadioButton ToolTip="Green" >
                                        <RadioButton.Content>
                                                <Rectangle Width="10" Height="10" Fill="Green"/>
                                        </RadioButton.Content>
                                </RadioButton>
                                <RadioButton ToolTip="Purple" >
                                        <RadioButton.Content>
                                                <Rectangle Width="10" Height="10" Fill="Purple"/>
                                        </RadioButton.Content>
                                </RadioButton>
                        </ToolBar>
                        <ToolBar Band="0" BandIndex="0" HorizontalAlignment="Left" Height="29" VerticalAlignment="Top" IsOverflowOpen="True" Margin="0,0,-5.2,0" Width="86" >
                                <RadioButton ToolTip="Gray">
                                        <RadioButton.Content>
                                                <Rectangle Width="10" Height="10" Fill="Gray"/>
                                        </RadioButton.Content>
                                </RadioButton>
                                <RadioButton  ToolTip="Orange" >
                                        <RadioButton.Content>
                                                <Rectangle Width="10" Height="10" Fill="Orange"/>
                                        </RadioButton.Content>
                                </RadioButton>
                                <RadioButton ToolTip="SeaGreen" >
                                        <RadioButton.Content>
                                                <Rectangle Width="10" Height="10" Fill="SeaGreen"/>
                                        </RadioButton.Content>
                                </RadioButton>
                                <RadioButton ToolTip="Purple" >
                                        <RadioButton.Content>
                                                <Rectangle Width="10" Height="10" Fill="YellowGreen"/>
                                        </RadioButton.Content>
                                </RadioButton>
                        </ToolBar>
                        <ToolBar HorizontalAlignment="Left" Height="47" VerticalAlignment="Top" IsOverflowOpen="True" Band="1" BandIndex="0" Margin="0,-0.8,-30.2,0" Width="95"   >
                                <Button Content="新建"/>
                                <Button Content="保存"/>
                        </ToolBar>
                        <ToolBar HorizontalAlignment="Left" Height="39" VerticalAlignment="Top" IsOverflowOpen="True" Band="1" BandIndex="1" Margin="0.2,0.2,-42.6,0" Width="120"  >
                                <Button Content="复制"/>
                                <Button Content="剪切"/>
                        </ToolBar>
                </ToolBarTray>
        </Grid>
</Window>