首页
学习
活动
专区
圈层
工具
发布
50 篇文章
1
【愚公系列】2023年09月 WPF控件专题 XAML介绍
2
【愚公系列】2023年09月 WPF控件专题 WPF应用程序组成
3
【愚公系列】2023年09月 WPF控件专题 Window窗体属性和事件
4
【愚公系列】2023年09月 WPF控件专题 Label、TextBox、PasswordBox控件介绍
5
【愚公系列】2023年09月 WPF控件专题 Button控件详解
6
【愚公系列】2023年09月 WPF控件专题 RadioButton控件详解
7
【愚公系列】2023年09月 WPF控件专题 CheckBox控件详解
8
【愚公系列】2023年09月 WPF控件专题 Image控件详解
9
【愚公系列】2023年09月 WPF控件专题 Border控件详解
10
【愚公系列】2023年09月 WPF控件专题 ComboBox控件详解
11
【愚公系列】2023年09月 WPF控件专题 ListBox控件详解
12
【愚公系列】2023年09月 WPF控件专题 DatePicker控件详解
13
【愚公系列】2023年09月 WPF控件专题 Calendar控件详解
14
【愚公系列】2023年09月 WPF控件专题 Slider控件详解
15
【愚公系列】2023年09月 WPF控件专题 ProgressBar控件详解
16
【愚公系列】2023年10月 WPF控件专题 StackPanel控件详解
17
【愚公系列】2023年10月 WPF控件专题 WrapPanel控件详解
18
【愚公系列】2023年10月 WPF控件专题 DockPanel控件详解
19
【愚公系列】2023年10月 WPF控件专题 Canvas控件详解
20
【愚公系列】2023年10月 WPF控件专题 Grid控件详解
21
【愚公系列】2023年10月 WPF控件专题 Groupbox控件详解
22
【愚公系列】2023年10月 WPF控件专题 Expander控件详解
23
【愚公系列】2023年10月 WPF控件专题 TabControl控件详解
24
【愚公系列】2023年10月 WPF控件专题 Frame控件详解
25
【愚公系列】2023年10月 WPF控件专题 ListView控件详解
26
【愚公系列】2023年10月 WPF控件专题 DataGrid控件详解
27
【愚公系列】2023年10月 WPF控件专题 Menu控件详解
28
【愚公系列】2023年10月 WPF控件专题 ContextMenu控件详解
29
【愚公系列】2023年10月 WPF控件专题 TreeView控件详解
30
【愚公系列】2023年10月 WPF控件专题 ToolBar控件详解
31
【愚公系列】2023年10月 WPF控件专题 ToolBarTray控件详解
32
【愚公系列】2023年10月 WPF控件专题 StatusBar控件详解
33
【愚公系列】2023年11月 WPF控件专题 MediaElement控件详解
34
【愚公系列】2023年11月 WPF控件专题 RichTextBox控件详解
35
【愚公系列】2023年11月 WPF控件专题 GridView控件详解
36
【愚公系列】2023年11月 WPF控件专题 Rectangle控件详解
37
【愚公系列】2023年11月 WPF控件专题 Popup控件详解
38
【愚公系列】2023年11月 WPF控件专题 OpenFileDialog控件详解
39
【愚公系列】2023年11月 WPF控件专题 SaveFileDialog控件详解
40
【愚公系列】2023年11月 WPF控件专题 RepeatButton控件详解
41
【愚公系列】2023年11月 WPF控件专题 Ellipse控件详解
42
【愚公系列】2023年11月 WPF控件专题 Polygon控件详解
43
【愚公系列】2023年11月 WPF控件专题 Path控件详解
44
【愚公系列】2023年11月 WPF控件专题 WindowFormsHost控件详解
45
【愚公系列】2023年11月 WPF控件专题 WebBrowser控件详解
46
【愚公系列】2023年11月 WPF控件专题 Validation控件详解
47
【愚公系列】2023年11月 WPF控件专题 Page控件详解
48
【愚公系列】2023年11月 WPF控件专题 PrintDialog控件详解
49
【愚公系列】2023年11月 WPF控件专题 Track控件详解
50
【愚公系列】2023年11月 WPF控件专题 Polyline控件详解

【愚公系列】2023年10月 WPF控件专题 ToolBarTray控件详解

🚀前言

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

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

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

🚀一、ToolBarTray控件详解

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

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

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

代码语言:txt
复制
<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.具体案例

代码语言:javascript
复制
<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>

我正在参与2023腾讯技术创作特训营第三期有奖征文,组队打卡瓜分大奖!

下一篇
举报
领券