2009-09-30 102 views

回答

10

它實際上是一個切換按鈕,我檢查了SimpleStyles項目TreeView的模板,這是我發現:

<ControlTemplate TargetType="ToggleButton"> 
     <Grid 
     Width="15" 
     Height="13" 
     Background="Transparent"> 
     <Path x:Name="ExpandPath" 
      HorizontalAlignment="Left" 
      VerticalAlignment="Center" 
      Margin="1,1,1,1" 
      Fill="{StaticResource GlyphBrush}" 
      Data="M 4 0 L 8 4 L 4 8 Z"/> 
     </Grid> 
     <ControlTemplate.Triggers> 
     <Trigger Property="IsChecked" 
      Value="True"> 
      <Setter Property="Data" 
       TargetName="ExpandPath" 
       Value="M 0 4 L 8 4 L 4 8 Z"/> 
     </Trigger> 
     </ControlTemplate.Triggers> 
    </ControlTemplate> 

那麼這就是你需要做什麼,使其工作:

<Window x:Class="StackOverflowTests.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" x:Name="window1" Height="300" Width="300" 
Loaded="window1_Loaded" 
xmlns:local="clr-namespace:StackOverflowTests"> 
<Window.Resources> 
    <SolidColorBrush x:Key="GlyphBrush" Color="#444" /> 
    <ControlTemplate x:Key="toggleButtonTemplate" TargetType="ToggleButton"> 
    <Grid 
      Width="15" 
      Height="13" 
      Background="Transparent"> 
    <Path x:Name="ExpandPath" 
       HorizontalAlignment="Left" 
       VerticalAlignment="Center" 
       Margin="1,1,1,1" 
       Fill="{StaticResource GlyphBrush}" 
       Data="M 4 0 L 8 4 L 4 8 Z"/> 
    </Grid> 
    <ControlTemplate.Triggers> 
    <Trigger Property="IsChecked" 
       Value="True"> 
    <Setter Property="Data" 
        TargetName="ExpandPath" 
        Value="M 0 4 L 8 4 L 4 8 Z"/> 
    </Trigger> 
    </ControlTemplate.Triggers> 
    </ControlTemplate> 
    <Style x:Key="toggleButtonStyle" TargetType="ToggleButton"> 
    <Setter Property="Template" Value="{StaticResource toggleButtonTemplate}" /> 
    </Style> 
</Window.Resources> 
<StackPanel> 
    <ToggleButton x:Name="toggleButton" Height="20" Width="20" Style="{StaticResource toggleButtonStyle}" /> 
</StackPanel> 
</Window> 
  • 首先你將模板(toggleButtonTemplate)放到你的資源中
  • 然後你製作一個樣式(toggleButtonStyle)tha T臺控制
  • 的模板(toggleButtonTemplate)最後你告訴你的切換按鈕其風格toggleButtonStyle

如果你只是從它應該直出作品的拷貝粘貼。

這是一個簡單的過程,但如果您不習慣使用模板,它會讓您頭疼,如果您有任何問題,請告訴我。

要了解一點的路徑迷你語言:

Geometry mini-language

+0

意想不到的答案,謝謝! – 2009-09-30 16:51:17

+0

有什麼辦法可以爲操作系統使用系統默認圖形?例如在Windows XP下,它是[+],而在Vista下則是三角形。 – 2009-09-30 16:59:45

+0

您可以隨時更改「ExpandPath」路徑的數據屬性(默認值和觸發器)。我正在談論帶有「M 0 4 L ...」代碼的字符串(稱爲幾何迷你語言,我在答案結尾添加了一個鏈接)。儘管如此,你還是需要了解路徑和迷你語言,這些我都沒有多大用處。但是請嘗試修改這些值,然後您會看到擴展和未擴展的數字如何變化。 – Carlo 2009-09-30 17:40:05