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

猜你喜欢

转载自blog.csdn.net/kenjianqi1647/article/details/82709457