2021-08-23 WPF控件专题 ToolBar 控件详解

本文介绍了WPF中的ToolBar控件,它作为HeaderedItemsControl的子类,常用于放置工具按钮。文章展示了ToolBar的属性如Orientation和IsOverflowOpen,并提供了一个实际案例,包括新建按钮、图片、复选框、单选按钮和下拉框等元素的添加。通过设置静态资源命令,实现了点击不同颜色的单选按钮改变Label文本颜色的功能。此外,还包含一个ComboBox项点击事件,用于更改Label内容。代码中还包括新建窗口功能,展示了WPF窗口间的交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

1.ToolBar 控件介绍

父类:HeaderedItemsControl MenuItem TreeViewItem —条目控件

属性:Orientation 指示排列方向(只读)
Band BandIndex IsOverflowOpen

ToolBarTray :呈放多个ToolBar 位置 索引位置

2.具体案例

<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>

/// <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();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值