我有切换按钮,并且想为它们创建样式树。目标是拥有一个主要的样式和控制模板(使用TemplateBinding的) - 然后每个不同的按钮都有它自己的样式,我可以简单地改变一些Setter属性来调整我需要的任何东西。这节省了我必须为每个按钮制作一个可能巨大的控制模板。这也节省了时间,当我想改变他们所有的样子。使用切换按钮,我希望在其中有两条路径,并且使用IsChecked触发器来更改不透明度(Path1将为默认值,当选中时Path2将为可见值)。但是我无法区分两个Path.Data,因为迷你风格不会接受TargetName,并且触发器不接受TemplateBindings。如何区分具有相同名称的TemplateBindings
实施例:
<Style x:Key="DefaultToggleButton" TargetType="{x:Type ToggleButton}">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="0,0,1,1"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Border.Padding" Value="1"/>
<Setter Property="Path.Fill" Value="{DynamicResource FillBrush}"/>
<Setter Property="Path.StrokeThickness" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Viewbox
x:Name="viewbox"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Width="Auto"
Height="Auto">
<Border
SnapsToDevicePixels="true"
Width="16"
Height="16"
BorderThickness="0"
Background="{TemplateBinding Background}"
x:Name="border"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Padding="{TemplateBinding Border.Padding}">
<Grid
HorizontalAlignment="Center"
VerticalAlignment="Center"
Width="Auto"
Height="Auto">
<Path
Opacity="1"
Margin="2"
Name="ButtonPath1"
Fill="{TemplateBinding Path.Fill}"
Stretch="Fill"
StrokeEndLineCap="Round"
StrokeStartLineCap="Round"
StrokeThickness="{TemplateBinding Path.StrokeThickness}"
Data="{TemplateBinding Path.Data}" <!-- This one -->
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
<Path
Opacity="0"
Margin="2"
Name="ButtonPath2"
Fill="{TemplateBinding Path.Fill}"
Stretch="Fill"
StrokeEndLineCap="Round"
StrokeStartLineCap="Round"
StrokeThickness="{TemplateBinding Path.StrokeThickness}"
Data="{TemplateBinding Path.Data}" <!-- And this one -->
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</Border>
</Viewbox>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="ButtonPath1" Property="Opacity" Value="0"/>
<Setter TargetName="ButtonPath2" Property="Opacity" Value="1"/>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter TargetName="ButtonPath1" Property="Opacity" Value="1"/>
<Setter TargetName="ButtonPath2" Property="Opacity" Value="0"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style
x:Key="ActualButton"
TargetType="{x:Type ToggleButton}"
BasedOn="{StaticResource DefaultToggleButton}">
<!-- How can I point the properties below to the correct Path.Data?
Setting TargetName did not work since it is in a different Style. -->
<Setter Property="Path.Data" Value="M 0 0 L 6 0 L 3 6 Z"/> <!-- ButtonPath -->
<Setter Property="Path.Data" Value="M 0 6 L 3 0 L 6 6 Z"/> <!-- ButtonPath2 -->
</Style>
感谢。我想了一会儿这个建议,但最终找到了另一种方式。 –