重复使用TreeView的展开[+]并折叠WPF中的[ - ]按钮
问题描述:
有没有办法重用简单展开[+]
并折叠出现在WPF TreeView
中的节点旁边的按钮[-]
?我想在我的应用程序的其他地方有类似的图形来扩展和折叠一些控件。重复使用TreeView的展开[+]并折叠WPF中的[ - ]按钮
答
它实际上是一个切换按钮,我检查了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="*Tests.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:*Tests">
<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
如果你只是从它应该直出作品的拷贝粘贴。
这是一个简单的过程,但如果您不习惯使用模板,它会让您头疼,如果您有任何问题,请告诉我。
要了解一点的路径迷你语言:
意想不到的答案,谢谢! – 2009-09-30 16:51:17
有什么办法可以为操作系统使用系统默认图形?例如在Windows XP下,它是[+],而在Vista下则是三角形。 – 2009-09-30 16:59:45
您可以随时更改“ExpandPath”路径的数据属性(默认值和触发器)。我正在谈论带有“M 0 4 L ...”代码的字符串(称为几何迷你语言,我在答案结尾添加了一个链接)。尽管如此,你还是需要了解路径和迷你语言,这些我都没有多大用处。但是请尝试修改这些值,然后您会看到扩展和未扩展的数字如何变化。 – Carlo 2009-09-30 17:40:05