如何在代码后面访问控件模板?

时间:2014-05-01 11:57:10

标签: c# windows-phone-8

我是一位新的Windows Phone开发人员,我有很多问题。我现在的问题是这样的:

我有这段代码:

<Button x:Name="InfoEllipse" 
                Foreground="White"
                HorizontalAlignment="Right"
                VerticalAlignment="Center"   
                Margin="0,0,25,0" 
                MinWidth="34" MinHeight="31" 
                   Click="InfoEllipse_Click">
            <Button.Template>
                <ControlTemplate x:Name="EllipseControlTemplate">
                    <Border x:Name="EllipseButtonBorder"
                            BorderBrush="#4387C4" 
                        Background="#4387C4" 
                        CornerRadius="20,20,20,20"
                            BorderThickness="2">
                    </Border>
                </ControlTemplate>
            </Button.Template>
        </Button>

所有这些代码都放在UserControl xaml中。我的问题是我想访问我的边框 - &gt; &#39; EllipseButtonBorder&#39;在代码后面,这样当我点击这个按钮来改变这个边框背景。问题是我无法弄清楚如何在后面的代码中访问此EllipseButtonBorder?请帮忙。我使用的是Windows Phone 8。

1 个答案:

答案 0 :(得分:0)

您应该使用VisualState来管理按钮的状态。

<Button x:Name="InfoEllipse" 
    Foreground="White"
    HorizontalAlignment="Right"
    VerticalAlignment="Center"   
    Margin="0,0,25,0" 
    MinWidth="34" MinHeight="31" 
    Click="InfoEllipse_Click">
    <Button.Template>
        <ControlTemplate x:Name="EllipseControlTemplate">
            <Border x:Name="EllipseButtonBorder"
                BorderBrush="#4387C4" 
                Background="#4387C4" 
                CornerRadius="20,20,20,20"
                BorderThickness="2">
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal"/>
                        <VisualState x:Name="MouseOver"/>
                        <VisualState x:Name="Pressed">
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="EllipseButtonBorder">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="Green"/>
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </VisualState>
                        <VisualState x:Name="Disabled" />
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
            </Border>
        </ControlTemplate>
    </Button.Template>
</Button>