В Silverlight, как я могу получить анимацию из шаблона и использовать ее через код C #? - PullRequest
2 голосов
/ 16 июля 2010

У меня есть шаблон элемента управления, который выглядит следующим образом:

<ControlTemplate x:Key="anchorButton" TargetType="Button">
            <Grid x:Name="CommonGrid" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
                <Image x:Name="CommonPic" Source="anchor.png" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Opacity="1"/> 
                <Image x:Name="CommonPicSelected" Source="anchorSelected.png" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Opacity="0"/> 
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="FocusStates">
                        <VisualState x:Name="Focused"/>
                        <VisualState x:Name="Unfocused"/>
                    </VisualStateGroup>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal"/>
                        <VisualState x:Name="MouseOver">
                            <Storyboard>
                                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="CommonPic" Storyboard.TargetProperty="(UIElement.Opacity)">
                                    <EasingDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
                                </DoubleAnimationUsingKeyFrames>    
                                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="CommonPicSelected" Storyboard.TargetProperty="(UIElement.Opacity)">
                                    <EasingDoubleKeyFrame KeyTime="00:00:00" Value="1"/>
                                </DoubleAnimationUsingKeyFrames>                            
                            </Storyboard>
                        </VisualState>
                        <VisualState x:Name="Pressed"/>
                        <VisualState x:Name="Disabled"/>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
            </Grid>
        </ControlTemplate>

Находясь в приложении, я могу изменять изображения, когда нажимаю кнопку мыши над кнопкой, но мне нужно сделать это с помощью кода(Я вызываю JavaScript, когда я нащупываю указатель мыши над строкой, соответствующая точка в моем приложении Silverlight должна быть выделена).

Ссылка на JavaScript уже сделана:

 [ScriptableMember]
 public void UpdateText(int result)
 {
     for (int i = 0; i < 4; i++)
     {
         ButtonBase button = (ButtonBase)VisualTreeHelper.GetChild(RegionCanvas, i);

         if (button.DataContext.ToString().Equals("" + result))
         {
             HtmlPage.Window.Invoke("highlightRow", button.DataContext);
         }
         else
         {
             HtmlPage.Window.Invoke("unHighlightRow", button.DataContext);
         }
    }
}

Iхотел использовать эту анимацию в визуальном состоянии и использовать внутри кода, показанного выше.Это возможно?Как?Если это не так, есть ли другой способ заставить его работать?

1 Ответ

1 голос
/ 22 июля 2010

Вам просто нужно назвать StoryBoard в XAML:

<Storyboard x:Name="storyboard">
                        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="CommonPic" Storyboard.TargetProperty="(UIElement.Opacity)">
                            <EasingDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
                        </DoubleAnimationUsingKeyFrames>
                        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="CommonPicSelected" Storyboard.TargetProperty="(UIElement.Opacity)">
                            <EasingDoubleKeyFrame KeyTime="00:00:00" Value="1"/>
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>

И тогда вы можете вызвать метод Begin из кода:

storyboard.Begin();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...