English 中文(简体)
Storyboard animation based on control template s control property [duplicate]
原标题:

This is what I have

  1. A ControlTemplate for a button in my window.resource, having an ellipse with an outerglow (named -- TasksToggleButtonOuterGlowBitmapEffect) and a text
  2. A button that is using this template

This is what I need

1. A storyboard that operates on the outerglow of the above mentioned ellipse 2. I shall trigger this storyboard from my codebehind file at any moment

When I tried this with the following snippet, the framework gives me a runtime exception stating that it is not able to find the control, TasksToggleButtonOuterGlowBitmapEffect

<Window.Resource>
    <ControlTemplate x:Key="DefaultTasksToggleButtonTemplate" TargetType="ToggleButton">
        <Grid Margin="2">
            <Border BorderBrush="White" BorderThickness="2" CornerRadius="20">
                <Border.BitmapEffect>
                    <OuterGlowBitmapEffect x:Name="TasksToggleButtonOuterGlowBitmapEffect" GlowColor="LightGray" />
                </Border.BitmapEffect>
            </Border>
            <Ellipse Fill="Red" Width="20" Height="20" Margin="2" />
            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
        </Grid>
    </ControlTemplate>


    <Storyboard x:Key="GlowStoryboard">
        <DoubleAnimation Storyboard.TargetName="TasksToggleButtonOuterGlowBitmapEffect" Storyboard.TargetProperty="GlowSize" From="5" To="10" />
    </Storyboard>

</Window.Resources>

Update -- I want this to be in the resource so that any button can use it

问题回答

On the surface, this feels to me like a good candidate for using Visual State Manager. Animations are a little heavy handed for this sort of effect IF the trigger is going to be something like MouseOver, Pressed, etc. If you plan on reacting to Button State, then definitely look at VSM.

The problem you are having is that the target you are trying to access is part of a template, and therefore not a real element. It makes sense that you cannot do it this way: imagine you had 50 buttons all applying the same template: how would the animation know which TasksToggleButtonOuterGlowBitmapEffect you really meant?

You may be able to handle this in a Trigger (if this is WPF), but I think to do so the animation would need to be defined within the Template as well, and I don t know if you can do that.

You also may be able to navigate the template tree at runtime in code behind and setup your animation there.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签