有没有办法重用出现在 WPF 中节点旁边的简单展开[+]
和折叠按钮?我想在我的应用程序的其他地方有一个类似的图形来扩展和折叠一些控件。[-]
TreeView
问问题
5504 次
1 回答
10
它实际上是一个 ToggleButton,我检查了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)来设置控件的模板(toggleButtonTemplate)
- 最后你告诉你的 ToggleButton 它的样式是 toggleButtonStyle
如果你只是复制粘贴到它应该直接工作。
这是一个简单的过程,但如果您不习惯使用模板,它会让您头疼,如果您有任何问题,请告诉我。
要了解一些关于路径迷你语言的知识:
于 2009-09-30T16:19:13.953 回答