故事板访问controltemplate中的属性

时间:2014-03-08 13:53:19

标签: c# animation mvvm windows-phone-8 storyboard

我在访问controltemplate中的属性时遇到以下问题。我有以下controltemplate:

<Button BorderBrush="Transparent" BorderThickness="0" Command="{Binding PopUpOpenCommand}" CommandParameter="{Binding}" Name="OpenPopUp">
            <Button.Template>
                <ControlTemplate>
                    <Path x:Name="CountryUser" Stretch="Fill" StrokeThickness="{StaticResource StrokeUserControl}"  StrokeLineJoin="Round" Stroke="{Binding CountryView.CountryColor}" Data="{Binding CountryView.MapData}" Fill="{StaticResource CountryBackground}"/>
                </ControlTemplate>
            </Button.Template>
        </Button>

我想让以下故事板更改controltemplate中的元素。

<Storyboard x:Name="storyboard1">
     <ColorAnimation Storyboard.TargetName="CountryUser" Storyboard.TargetProperty="Stroke" To="Blue"/>
</Storyboard>

如何在XAML中执行此操作?

从后面的代码我发现了Storyboards cant find ControlTemplate elments。我可以毫无问题地找到我的MVVM结构中的元素。但似乎答案不适用于Windows Phone?因为我无法用两个输入开始故事板?

另一个想法

我在模板中设置了故事板。但我有问题我无法通过我的MVVM结构访问资源。这是代码:

    <Button BorderBrush="Transparent" BorderThickness="0" Command="{Binding PopUpOpenCommand}" CommandParameter="{Binding}" Name="OpenPopUp">
        <Button.Template>
            <ControlTemplate>
                <Path x:Name="CountryUser" Stretch="Fill" StrokeThickness="{StaticResource StrokeUserControl}"  StrokeLineJoin="Round" Stroke="{Binding CountryView.CountryColor}" Data="{Binding CountryView.MapData}" Fill="{StaticResource CountryBackground}">
                      <Storyboard x:Name="storyboard1">
                           <ColorAnimation Storyboard.TargetName="CountryUser" Storyboard.TargetProperty="Stroke" To="Blue"/>
                      </Storyboard> 
                </Path>
            </ControlTemplate>
        </Button.Template>
    </Button>

但是如何才能访问故事板。我不能使用Template.Find,因为这在Windows手机上不存在?

1 个答案:

答案 0 :(得分:0)

<Button x:Name="Button1" BorderThickness="0" BorderBrush="Transparent">
<Button.Template>
    <ControlTemplate x:Name="Control">
        <Path x:Name="CountryUser" Style="{StaticResource style_ColorButton}" Data="{Binding UserView.buttonData}" Fill="{StaticResource ButtonBackground}">
            <Path.Resources>
                <Storyboard x:Name="StoryBoard1">
                    <ColorAnimation Storyboard.TargetName="User" Storyboard.TargetProperty="(Stroke).(SolidColorBrush.Color)" To="Blue" Duration="0"/>
                </Storyboard>
            </Path.Resources>
        </Path>
    </ControlTemplate> 
</Button.Template>

和激活

foreach (UIElement x in ElementsAtPoint)
{
    f = x as FrameworkElement;
    if (f is Path)
    {
        try { 
        h = f as Path;
        Storyboard sb = h.Resources["StoryBoard1"] as Storyboard;
        sb.Begin();
            }
        catch
        {

        }
        break;
    }
}
相关问题