首页
学习
活动
专区
圈层
工具
发布
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控件专题 ToolBar控件详解

🏆 作者简介,愚公搬代码 🏆《头衔》:华为云特约编辑,华为云云享专家,华为开发者专家,华为产品云测专家,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#等编程语言来创建个性化的用户界面元素。自定义控件可以根据需求提供更多的功能和自定义化选项,以及更好的用户体验。

🚀一、ToolBar控件详解

在WPF中,ToolBar控件是一个非常常用的控件。ToolBar控件通常用来放置一系列可以执行命令的按钮或其他控件,常见的应用场景是如Microsoft Office等办公软件中的工具栏。

以下是ToolBar控件的一些常用属性:

  • HorizontalAlignment:水平对齐方式。
  • VerticalAlignment:垂直对齐方式。
  • Orientation:控件排列方向,可以是水平或垂直。
  • IsOverflowOpen:表示是否展开显示溢出的项目。
  • OverflowMode:指定溢出项目的应对方式。

以下是一个简单的例子,展示如何在XAML中使用ToolBar控件:

代码语言:html
复制
<ToolBar Orientation="Horizontal">
    <Button Content="New" />
    <Button Content="Open" />
    <Button Content="Save" />
</ToolBar>

在上面的例子中,我们创建了一个水平排列的ToolBar控件,并在其中添加了三个按钮,分别对应“新建”、“打开”和“保存”操作。

🔎1.属性介绍

ToolBar控件是WPF中常用的控件之一,以下是ToolBar控件常用属性的介绍:

  1. IsOverflowOpen:用于控制ToolBar的溢出按钮是否展开。
  2. Orientation:用于设置ToolBar的方向,可选值为Horizontal(水平)和Vertical(垂直)。
  3. Band:用于设置ToolBar在工具栏中的位置。
  4. ItemsSource:用于绑定ToolBar的Items属性的数据源。
  5. OverflowMode:用于设置ToolBar溢出时的展现方式,可选值为AsNeeded(按需展现)和Always(总是展现)。
  6. ButtonStyle:用于设置ToolBar中的按钮的样式。
  7. ToolBarTray.IsLocked:用于控制ToolBar是否可以移动。
  8. ToolBarTray.Orientation:用于设置ToolBarTray的方向,可选值为Horizontal(水平)和Vertical(垂直)。

🔎2.常用场景

  1. 常规工具条:在窗口顶部或底部放置一些常用的操作按钮,例如撤销、重做、保存、打印等。
  2. 编辑工具条:在编辑页或文本编辑器中使用工具条来展示编辑工具,例如字体、颜色、段落格式等。
  3. 导航工具条:在复杂的应用程序中使用工具条帮助用户浏览不同的页面和区域,例如一个具有多个选项卡的应用程序,每个选项卡对应一个不同的页面。
  4. 状态工具条:在应用程序中展示当前的状态信息,例如当前的网速、CPU使用率等。
  5. 排版工具条:在处理排版和布局的应用程序中使用工具条,例如桌面出版、图形设计等。
  6. 自定义工具条:可以根据不同的应用程序需求自定义工具条,例如扫描仪软件中的设置工具条。

🔎3.具体案例

代码语言:html
复制
<Window x:Class="WpfAppTest.ToolBarWindow"
        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="ToolBarWindow" Height="450" Width="800">
        <Window.Resources>
                <RoutedUICommand x:Key="setColor"/>
        </Window.Resources>
        <Window.CommandBindings>
                <CommandBinding Command="{StaticResource setColor}" Executed="SetLabelColor"/>
        </Window.CommandBindings>
        <Grid>
                <ToolBar HorizontalAlignment="Left" Height="36" Margin="10,10,0,0" VerticalAlignment="Top" Width="207" IsOverflowOpen="True" >
                        <Button Name="btnNew" Content="新建"  Click="BtnNew_Click"/>
                        <Separator/>
                        <Image Source="imgs/1111.jpg" Height="20" Width="20"/>
                        <CheckBox Content=" 是否保存" IsChecked="True" Margin="10,0"/>
                        <Separator/>
                        <RadioButton Name="rbRed" ToolTip="Red" Command="{StaticResource setColor}">
                                <RadioButton.Content>
                                        <Rectangle Width="10" Height="10" Fill="Red"/>
                                </RadioButton.Content>
                        </RadioButton>
                        <RadioButton  ToolTip="Yellow" Command="{StaticResource setColor}" >
                                <RadioButton.Content>
                                        <Rectangle Width="10" Height="10" Fill="Yellow"/>
                                </RadioButton.Content>
                        </RadioButton>
                        <RadioButton ToolTip="Green" Command="{StaticResource setColor}">
                                <RadioButton.Content>
                                        <Rectangle Width="10" Height="10" Fill="Green"/>
                                </RadioButton.Content>
                        </RadioButton>
                        <RadioButton ToolTip="Purple" Command="{StaticResource setColor}">
                                <RadioButton.Content>
                                        <Rectangle Width="10" Height="10" Fill="Purple"/>
                                </RadioButton.Content>
                        </RadioButton>
                        <Separator/>
                        <ComboBoxItem Content="选择" VerticalContentAlignment="Center" PreviewMouseLeftButtonUp="ComboBoxItem_MouseLeftButtonUp"/>

                </ToolBar>
                <Label Name="lbl" Content="我是测试文本" HorizontalAlignment="Left" Margin="103,78,0,0" VerticalAlignment="Top"/>


        </Grid>
</Window>
代码语言:javascript
复制
/// <summary>
/// 设置文本颜色 
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SetLabelColor(object sender,RoutedEventArgs e)
{
        RadioButton rb = e.Source as RadioButton;
        Rectangle rect = rb.Content as Rectangle;
        lbl.Foreground = rect.Fill;
}

private void ComboBoxItem_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
        lbl.Content = "罗丽";
}

private void BtnNew_Click(object sender, RoutedEventArgs e)
{
        Window newWin = new Window();
        newWin.Title = "新建页面";
        newWin.Show();
}

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

下一篇
举报
领券