1. 程式人生 > >WPF下ToolBar控制元件的使用

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