tags:

views:

52

answers:

1

Hi,

I have a button which in Blend I have edited (using Edit current template). I have added my animations for mouse over etc and the button works as expected.

However, on the mouse over event I have a shape that scales. What I want to do is give the user the option to set in XAML the rotation and scaling properties.

So for example, something like this:

<Button Height="76" Content="Gallery" Style="{StaticResource RotatingAnimationButton}" " Scaling="2.0" >

where in the template I have :

<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Document" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0]. (ScaleTransform.ScaleX)">

where Value="1.5" would be changed to "2.0".

Currently, all I have is the style of the template. I am not sure whether I can pass in the parameters or I have to create some sort of user control?

JD

Note : This question was originally posted as Silverlight and WPF. But as you will see it only applies to Silverlight which is why the excellent solutions provided caused me problems.

+2  A: 

You have two good options:

  1. You can subclass Button and add a "Scaling" property, or
  2. You can create an attached "Scaling" property and attach it to Button

In either case, your animation can bind to it.

Unless your button is custom in other ways, I would generally go with the attached property. You would use a standard attached property template (use the "propa" snippet):

public class MyAttachedProperties
{
      // Scaling
  public static double GetScaling(DependencyObject obj) { return (double)obj.GetValue(ScalingProperty); }
  public static void SetScaling(DependencyObject obj, double value) { obj.SetValue(ScalingProperty, value); }
  public static readonly DependencyProperty ScalingProperty = DependencyProperty.RegisterAttached("Scaling", typeof(double), typeof(MyAttachedProperties));
}

In the code that uses the button you would reference it like this:

<Button Height="76" Content="Gallery"
        Style="{StaticResource RotatingAnimationButton}"
        local:MyAttachedProperties.Scaling="2.0" />

In the template you would bind it like this:

Value="{Binding Path=(local:MyAttachedProperties.Scaling),
                RelativeSource={RelativeSource TemplatedParent}}"

Both of these bits of XAML assume you have xmlns:local defined to include the MyAttachedProperties class.

Ray Burns
+1 Correct options.
Danny Varod
@Ray: Thank you so much. With your first option you mentioned the "button is custom in other ways", can you explain more?
JD
You might consider subclassing Button or ButtonBase to add significant new functionality. For example I created a confirmation button class that pops up an "Are you sure" type dialog with custom text, button labels, colors, etc. It had enough custom functionality that it made sense to subclass rather than just adding attached properties. As another example, CheckBox is a really a kind of button (it derives from ButtonBase), as is GridViewColumnHeader.
Ray Burns
@Ray: Thank you so much for the explanation.
JD
@Ray: I am getting invalid XML in blend for Value="{...}". Also in VS2008 I get invalid attribute setting. If I remove Path= in VS2008, an error at runtime "invalid binding path character ':' in local:" is shown.
JD
Sorry - I left off the final closing brace. I'll edit my answer to add it.
Ray Burns
Yeah, I noticed that and did apply it but I still seem to be getting issues in both blend and vs2008.
JD
If you are getting "invalid XML" messages, then your XML itself is malformed. The `AttributeName="Value"` syntax requires both quotes to be present and for you to escape certain characters within the value string, but none of the special characters `{}(),.=` need escaping like this so they should all be valid inside the quotes. Is it possible that the rest of your element is malformed (eg missing `>` at the end)?
Ray Burns
@Ray: Still no luck. I have reposted again with all the code I am using here (http://stackoverflow.com/questions/2386264/what-is-wrong-with-the-custom-attached-property).
JD
@Ray. Thanks for all the help. After posting another question and googling for a bit, apparently this is not something you can do in Silverlight. Thanks once again as you have got me started in learning how to use attached properties.
JD