Search code examples
wpfpixelsense

How to set advanced style on custom surface button


I have made a custom suface button according this example:

<Button>
    <Button.Template>
        <ControlTemplate TargetType="Button">
            <Grid>
                <!-- my custom shape -->
                                    <Polygon
                                     Points="400,150 100,350 100,350, 500,350"
                                     Stroke="Purple" 
                                     StrokeThickness="2"
                                     Name="L1">
                                       <Polygon.Fill>
                                          <SolidColorBrush Color="Blue" Opacity="0.4"/>
                                       </Polygon.Fill>
                    </Polygon>
                <ContentPresenter />
            </Grid>
        </ControlTemplate>
    </Button.Template>
</Button>

But there is a problem with styles - there is no behaviour when touched/holded/released. Could you give me a clue how to do that?

thanks


Solution

  • An example control template (found at msdn) is:

    <Style TargetType="Button">
      <Setter Property="SnapsToDevicePixels" Value="true"/>
      <Setter Property="OverridesDefaultStyle" Value="true"/>
      <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
      <Setter Property="MinHeight" Value="23"/>
      <Setter Property="MinWidth" Value="75"/>
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="Button">
            <Border 
              x:Name="Border"  
              CornerRadius="2" 
              BorderThickness="1"
              Background="{StaticResource NormalBrush}"
              BorderBrush="{StaticResource NormalBorderBrush}">
              <ContentPresenter 
                Margin="2"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                RecognizesAccessKey="True"/>
            </Border>
            <ControlTemplate.Triggers>
              <Trigger Property="IsKeyboardFocused" Value="true">
                <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DefaultedBorderBrush}" />
              </Trigger>
              <Trigger Property="IsDefaulted" Value="true">
                <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DefaultedBorderBrush}" />
              </Trigger>
              <Trigger Property="IsMouseOver" Value="true">
                <Setter TargetName="Border" Property="Background" Value="{StaticResource DarkBrush}" />
              </Trigger>
              <Trigger Property="IsPressed" Value="true">
                <Setter TargetName="Border" Property="Background" Value="{StaticResource PressedBrush}" />
                <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource PressedBorderBrush}" />
              </Trigger>
              <Trigger Property="IsEnabled" Value="false">
                <Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" />
                <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}" />
                <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
              </Trigger>
            </ControlTemplate.Triggers>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
    

    As you can see you stripped out all the triggers, not to mention visual styles.

    I would take that code and put your polygon in place of the ContentPresenter.