0

I'm trying to create a custom button control with Blend (I have to admit that it's my first try with Blend) for my VS project.

1) I started by creating an empty WPF app in Blend an then modified the ControlTemplate of the button: I added a path and some eventbased coloring stuff:

<ControlTemplate x:Key="ButtonControlTemplate1" TargetType="{x:Type Button}">
        <ControlTemplate.Resources>
            <Storyboard x:Key="OnMouseEnter1">
                <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="path">
                    <EasingColorKeyFrame KeyTime="0:0:1" Value="#FF4242C5"/>
                </ColorAnimationUsingKeyFrames>
            </Storyboard>
            <Storyboard x:Key="OnMouseDown1">
                <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="path">
                    <EasingColorKeyFrame KeyTime="0" Value="#FF2D7957"/>
                </ColorAnimationUsingKeyFrames>
            </Storyboard>
        </ControlTemplate.Resources>
        <Grid>
            <Path x:Name="path" Data="M55.279307,7.1937681 L71.051681,25.658582 74.898866,11.040298 92.210133,27.966732 92.594819,36.045257 67.97435,43.7385 36.04509,43.7385 12.963697,42.584684 7.5777354,24.119657 C7.5777354,24.119657 55.279307,7.1937681 55.279307,7.1937681 z" Fill="#FF2D2D79" HorizontalAlignment="Stretch" Margin="33.352,1.695,41.046,10" Stretch="Fill" Stroke="Black" Width="Auto"/>
        </Grid>
        <ControlTemplate.Triggers>
            <EventTrigger RoutedEvent="UIElement.MouseEnter" SourceName="path">
                <BeginStoryboard x:Name="OnMouseEnter1_BeginStoryboard1" Storyboard="{StaticResource OnMouseEnter1}"/>
            </EventTrigger>
            <EventTrigger RoutedEvent="UIElement.MouseLeave" SourceName="path">
                <StopStoryboard BeginStoryboardName="OnMouseEnter1_BeginStoryboard1"/>
            </EventTrigger>
            <EventTrigger RoutedEvent="UIElement.MouseDown" SourceName="path">
                <BeginStoryboard x:Name="OnMouseDown1_BeginStoryboard" Storyboard="{StaticResource OnMouseDown1}"/>
            </EventTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

2) Finally, I made this button into a UserControl

3) I placed the UserControl in my empty wpf application.

So far it works fine. But, my task is to provide a huge size of buttons, each button with a different shape (meaning the path of each button holds different coordinates).

What do you recommend as a best solution? My first idea would be to create a (Dependency?) property on the button, to modify the path externally. But how to achieve it as the path property is now part of the control template...

bluefox
  • 175
  • 3
  • 16
  • Possible duplicate of [Dynamic PAth Icon Buttons in a Collection ItemTemplate Resource w/ MouseOver](https://stackoverflow.com/questions/35781205/dynamic-path-icon-buttons-in-a-collection-itemtemplate-resource-w-mouseover) – Chris W. Aug 10 '17 at 14:07
  • You should find everything you need in that dupe. Main part to take is turning your Path data into resources and template binding the Tag property to allow you to swap out paths at the instance level. :) – Chris W. Aug 10 '17 at 14:07
  • Perfect! Thanks! – bluefox Aug 11 '17 at 11:49

0 Answers0