為ToolBar中的控制項設定Style

  • 1013
  • 0
  • 2013-12-02

摘要:為ToolBar中的控制項設定Style

原來要為ToolBar中的控制項(例如:Button)設定Style還要符合ToolBar的特定格式才行耶!

下面是從 msdn 中找到的說明,記下備查:

http://msdn.microsoft.com/zh-tw/library/bb613577(v=vs.100).aspx

HOW TO:工具列上的樣式控制項

.NET Framework 4
 
本主題尚未接受評分 為這個主題評分
 

 

ToolBar 會定義 ResourceKey 物件,用以指定 ToolBar 內控制項的樣式。 若要設定 ToolBar 中控制項的様式,請將樣式的 x:key 屬性設為 ToolBar 中定義的 ResourceKey

ToolBar 會定義下列 ResourceKey 物件:

下列範例會定義 ToolBar 內控制項的樣式。

 
<Window.Resources>

  <!--Styles for controls in a toolbar.-->
  <Style x:Key="{x:Static ToolBar.SeparatorStyleKey}" TargetType="Separator">
    <Setter Property="Background" Value="DarkBlue"/>
    <Setter Property="Width" Value="2"/>
  </Style>

  <Style x:Key="{x:Static ToolBar.ButtonStyleKey}" TargetType="Button">
    <Setter Property="Foreground" Value="Blue"/>
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
  </Style>

  <Style x:Key="{x:Static ToolBar.CheckBoxStyleKey}" TargetType="CheckBox">
    <Setter Property="Foreground" Value="DarkSlateBlue"/>
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
  </Style>

  <Style x:Key="{x:Static ToolBar.MenuStyleKey}" TargetType="Menu">
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="FontStyle" Value="Italic"/>
    <Setter Property="FontWeight" Value="Bold"/>
    <Setter Property="Background" Value="LightSteelBlue"/>
  </Style>

  <Style x:Key="{x:Static ToolBar.RadioButtonStyleKey}" TargetType="RadioButton">
    <Setter Property="Background" Value="LightSteelBlue"/>
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
  </Style>

  <Style x:Key="{x:Static ToolBar.TextBoxStyleKey}" TargetType="TextBox">
    <Setter Property="Background" Value="DarkBlue"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="FontStyle" Value="Italic"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="Width" Value="75"/>
  </Style>

  <Style x:Key="{x:Static ToolBar.ComboBoxStyleKey}" TargetType="ComboBox">
    <Setter Property="Background" Value="LightSteelBlue"/>
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="MinWidth" Value="60"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
  </Style>
  <!--*********Styles for controls that are not in a toolbar.*********-->
  <Style TargetType="Separator">
    <Setter Property="Background" Value="DarkBlue"/>
    <Setter Property="Width" Value="2"/>
  </Style>

  <Style TargetType="Button">
    <Setter Property="Foreground" Value="Blue"/>
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
  </Style>

  <Style TargetType="CheckBox">
    <Setter Property="Foreground" Value="DarkSlateBlue"/>
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
  </Style>

  <Style TargetType="Menu">
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="FontStyle" Value="Italic"/>
    <Setter Property="FontWeight" Value="Bold"/>
    <Setter Property="Background" Value="LightSteelBlue"/>
  </Style>

  <Style TargetType="RadioButton">
    <Setter Property="Background" Value="LightSteelBlue"/>
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
  </Style>

  <Style TargetType="TextBox">
    <Setter Property="Background" Value="DarkBlue"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="FontStyle" Value="Italic"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="Width" Value="75"/>
  </Style>

  <Style TargetType="ComboBox">
    <Setter Property="Background" Value="LightSteelBlue"/>
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="MinWidth" Value="60"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
  </Style>
</Window.Resources>


...


<ToolBarTray Margin="10,10,3,3" 
             Grid.Column="0" Grid.Row="2"
             Background="LightBlue">
  <ToolBar >
    <Button Content="Button 1"/>
    <Button Content="Button 2"/>
    <Separator/>
    <CheckBox Content="CheckBox 1"/>
    <CheckBox Content="CheckBox 2"/>
    <Separator/>
    <RadioButton>One</RadioButton>
    <RadioButton>Two</RadioButton>
    <Separator/>
    <ComboBox>
      <ComboBoxItem IsSelected="True">Item 1</ComboBoxItem>
      <ComboBoxItem>Item 2</ComboBoxItem>
      <ComboBoxItem>Item 3</ComboBoxItem>
      <ComboBoxItem>Item 4</ComboBoxItem>
    </ComboBox>
    <TextBox/>
    <Separator/>
    <Menu>
      <MenuItem Header="Menu">
        <MenuItem Header="File">
          <MenuItem Header="Copy"/>
          <MenuItem Header="Paste"/>
        </MenuItem>
      </MenuItem>
    </Menu>
  </ToolBar>
</ToolBarTray>