2014-06-25 61 views
0

我有切换按钮,并且想为它们创建样式树。目标是拥有一个主要的样式和控制模板(使用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> 

回答

0

这可能也可能不是一个好主意,但我设法通过将“IsChecked”触发器移动到Style.Triggers下的ActualButton(而不是Default)样式中。

实施例:

<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}" 
           HorizontalAlignment="Center" 
           VerticalAlignment="Center"/> 
         </Grid> 
        </Border> 
       </Viewbox> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<Style 
    x:Key="ActualButton" 
    TargetType="{x:Type ToggleButton}" 
    BasedOn="{StaticResource DefaultToggleButton}"> 
    <Setter Property="Path.Data" Value="M 0 0 L 6 0 L 3 6 Z"/> 
    <Style.Triggers> 
     <Trigger Property="IsChecked" Value="True"> 
      <Setter Property="Path.Data" Value="M 0 6 L 3 0 L 6 6 Z"/> 
     </Trigger> 
    </Style.Triggers>  
</Style> 
1

由于TemplateBinding需要从要应用的模板的实际控制值,因此它可以为不同的控件提供您的单个值不不同的值。因为您在TemplateBinding的两个不同位置使用Path.Data,它会在两处为您提供相同的值。如果你想要不同的值,然后绑定它与两个不同的属性。

我认为你可以通过为你创建一个类型为Path的自定义DependencyPropertyToggleButton。一个用于未检查,另一个用于检查。

+0

感谢。我想了一会儿这个建议,但最终找到了另一种方式。 –

相关问题