前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >【愚公系列】2023年10月 WPF控件专题 ToolBar控件详解

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

原创
作者头像
愚公搬代码
修改2023-11-01 08:09:39
修改2023-11-01 08:09:39
49500
代码可运行
举报
文章被收录于专栏:历史专栏历史专栏
运行总次数:0
代码可运行

🏆 作者简介,愚公搬代码 🏆《头衔》:华为云特约编辑,华为云云享专家,华为开发者专家,华为产品云测专家,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
代码运行次数:0
复制
/// <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腾讯技术创作特训营第三期有奖征文,组队打卡瓜分大奖!

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 🚀前言
  • 🚀一、ToolBar控件详解
    • 🔎1.属性介绍
    • 🔎2.常用场景
    • 🔎3.具体案例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档