2009-09-30 101 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