WPF下ToolBar控件的使用

效果

一、颜色ToolBar

1、XAML设置

<ToolBar Margin="120,21,230,328">
            <RadioButton ToolTip="Red" Click="ColorButton_Click">
                <Rectangle Width="10" Height="10" Fill="Red"/>
            </RadioButton>
            <RadioButton ToolTip="Orange" Click="ColorButton_Click">
                <Rectangle Width="10" Height="10" Fill="Orange"/>
            </RadioButton>
            <RadioButton ToolTip="Yellow" Click="ColorButton_Click">
                <Rectangle Width="10" Height="10" Fill="Yellow"/>
            </RadioButton>
            <RadioButton ToolTip="Green" Click="ColorButton_Click">
                <Rectangle Width="10" Height="10" Fill="Green"/>
            </RadioButton>
            <RadioButton ToolTip="Blue" Click="ColorButton_Click">
                <Rectangle Width="10" Height="10" Fill="Blue"/>
            </RadioButton>
            <RadioButton ToolTip="Purple" Click="ColorButton_Click">
                <Rectangle Width="10" Height="10" Fill="Purple"/>
            </RadioButton>
            <RadioButton ToolTip="Transparent" Click="ColorButton_Click">
                <Rectangle Width="10" Height="10" Fill="Transparent"/>
            </RadioButton>
        </ToolBar>

2、后台获得设置的颜色

       Color setline1 = Colors.Red;
  private void ColorButton_Click(object sender, RoutedEventArgs e)
        {
            setline1 = ((SolidColorBrush)((Rectangle)(sender as RadioButton).Content).Fill).Color;//要获得所选方块的颜色,需要将SolidColorBrush取Color
        }

二、线条ToolBar

<ToolBar Margin="272,19,0,330" HorizontalAlignment="Left" Width="128">
            <RadioButton ToolTip="1pt" Click="ThicknessButton_Click">
                <Rectangle Height="10pt" Width="1" Fill="Black"/>
            </RadioButton>
            <RadioButton ToolTip="1.2pt" Click="ThicknessButton_Click">
                <Rectangle Height="10pt" Width="1.2" Fill="Black"/>
            </RadioButton>
            <RadioButton ToolTip="1.5pt" Click="ThicknessButton_Click">
                <Rectangle  Height="10pt" Width="1.5" Fill="Black"/>
            </RadioButton>
            <RadioButton ToolTip="2pt" Click="ThicknessButton_Click">
                <Rectangle Height="10pt" Width="2" Fill="Black"/>
            </RadioButton>
            <RadioButton ToolTip="2.5pt" Click="ThicknessButton_Click">
                <Rectangle Height="10pt" Width="2.5" Fill="Black"/>
            </RadioButton>
            <RadioButton ToolTip="3pt" Click="ThicknessButton_Click">
                <Rectangle Height="10pt" Width="3" Fill="Black"/>
            </RadioButton>
            <RadioButton ToolTip="4pt" Click="ThicknessButton_Click">
                <Rectangle Height="10pt" Width="4" Fill="Black"/>
            </RadioButton>
            <RadioButton ToolTip="5pt" Click="ThicknessButton_Click">
                <Rectangle Height="10pt" Width="5" Fill="Black"/>
            </RadioButton>
            <RadioButton ToolTip="10pt" Click="ThicknessButton_Click">
                <Rectangle Height="10pt" Width="10" Fill="Black"/>
            </RadioButton>
        </ToolBar>

Rectangle设置显示为长方形,也可以设置成椭圆等其他形状。

2、获取所选长方形的宽度


//当前线粗细设置
private double thickness1 = 1;  
private void ThicknessButton_Click(object sender, RoutedEventArgs e)
   {
      thickness1 = ((Rectangle)(sender as RadioButton).Content).Width;
   }     

XAML设置为Rectangle,后台也要设置为Rectangle

<think>好的,用户想了解WPFToolBar的用法或者示例代码。我需要先回忆一下WPFToolBar的基本使用方法,然后整理出一个清晰的步骤指南。首先,ToolBar通常放在ToolBarTray中,这样可以支持拖放和排列。然后,ToolBar里包含各种按钮,比如Button、ToggleButton、Separator这些控件。可能需要提到如何添加图标,使用Image控件或者Path来显示矢量图标。还有样式的问题,默认的ToolBar可能会有多余的边框,可能需要调整样式。另外,用户可能想知道如何实现工具栏的溢出菜单,当空间不足时,部分按钮自动进入溢出区域。需要给出XAML代码示例,展示如何创建ToolBar,并添加常见的控件。同时,可以提到如何绑定命令,比如使用ICommand来关联按钮的点击事件。可能还需要提到如何自定义工具栏的外观,比如修改按钮的样式,调整间距等。另外,注意用户可能遇到的问题,比如按钮间距太大,或者图标不显示,这时候需要检查资源路径或者样式设置。最后,确保示例代码正确,并且符合WPF的最佳实践。可能需要分步骤讲解,从创建ToolBarTray开始,逐步添加控件,然后设置样式和命令绑定。还要考虑是否需要代码后台的处理,比如事件处理函数,或者完全通过MVVM模式来实现。如果有相关的引用资源,比如MSDN文档或者社区推荐的实践,可以添加引用标识。现在需要把所有这些内容组织成一个结构化的回答,确保用户能够一步步跟随并实现自己的ToolBar。</think>### WPF ToolBar 使用教程 在WPF中,`ToolBar`控件用于创建具有分组功能的工具栏,支持按钮、分隔符和下拉菜单等元素。以下是关键使用步骤: #### 1. 基础布局 ```xml <ToolBarTray> <!-- 支持多个工具栏拖拽排序 --> <ToolBar Header="标准工具"> <Button Command="ApplicationCommands.Cut"> <Image Source="Icons/cut.png" Width="16"/> </Button> <Separator/> <!-- 分组间隔线 --> <ToggleButton Command="local:CustomCommands.Bold"/> </ToolBar> </ToolBarTray> ``` * 使用`ToolBarTray`容器管理多个工具栏 * 通过`Header`属性设置工具栏标题 * 内置控件支持:`Button`, `ToggleButton`, `Separator`等 #### 2. 样式优化 默认样式可能包含多余间距,推荐添加以下样式: ```xml <Style TargetType="{x:Type ToolBar}"> <Setter Property="OverridesDefaultStyle" Value="True"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ToolBar}"> <ItemsPresenter/> </ControlTemplate> </Setter.Value> </Setter> </Style> ``` #### 3. 溢出菜单处理 当空间不足时自动创建溢出菜单: ```xml <ToolBar Band="1" BandIndex="1" OverflowMode="AlwaysOverflow"> <ComboBox Width="120" SelectedIndex="0"> <sys:String>Item 1</sys:String> <sys:String>Item 2</sys:String> </ComboBox> </ToolBar> ``` * `Band`控制工具栏排列顺序 * `OverflowMode`设置溢出策略 #### 4. 命令绑定 推荐使用MVVM模式: ```csharp public ICommand BoldCommand => new RelayCommand(() => { // 处理加粗逻辑 }); ``` XAML绑定: ```xml <ToggleButton Command="{Binding BoldCommand}" ToolTip="文字加粗"> <Path Data="M10 18l-6-6 6-6 1.4 1.4L6.8 12l4.6 4.6z" Fill="Black"/> </ToggleButton> ``` #### 5. 常见问题解决 - **图标不显示**:检查资源路径是否使用`pack://application`格式 - **按钮间距过大**:添加`<Style TargetType="{x:Type ToolBarPanel}">`修改`Margin` - **工具栏无法拖动**:确认是否包含在`ToolBarTray`中
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值