如何设置PhoneTextBoxs ActionIcon的样式

时间:2013-12-23 09:32:55

标签: c# xaml windows-phone-8

我正在开发一个Windows Phone 8应用程序,我需要一个带有ActionIcon的PhoneTextBox。当PhoneTextBox处于活动状态时,我将其设置为样式并且我成功地设置了ActionIcon的样式,但是我无法访问ActionIcon上的事件。

这是我的代码:

<Style TargetType="toolkit:PhoneTextBox">
        <Setter Property="Background" Value="{StaticResource Custom}" />
        <Setter Property="FontSize" Value="26" />
        <Setter Property="Foreground" Value="{StaticResource Inverted}" />
        <Setter Property="Hint" Value="{StaticResource Inverted}" />
        <Setter Property="BorderBrush" Value="{StaticResource Custom}" />
        <Setter Property="BorderThickness" Value="2" />
        <Setter Property="HintStyle">
            <Setter.Value>
                <Style TargetType="ContentControl">
                    <Setter Property="Foreground" Value="{StaticResource Inverted}"/>
                    <Setter Property="FontSize" Value="26"/>
                </Style>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="toolkit:PhoneTextBox">
                    <Grid Background="Transparent">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MainBorder" Storyboard.TargetProperty="Background" Duration="0">
                                            <DiscreteObjectKeyFrame Value="{StaticResource CustomDisableBorder}" KeyTime="0"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentElement" Storyboard.TargetProperty="Foreground" Duration="0">
                                            <DiscreteObjectKeyFrame Value="{StaticResource CustomDisableForeground}" KeyTime="0"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ActionIcon" Storyboard.TargetProperty="Source" Duration="0">
                                            <DiscreteObjectKeyFrame Value="/Images/cancel_light.png" KeyTime="0"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="FocusStates">
                                <VisualState x:Name="Focused" >
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MainBorder" Storyboard.TargetProperty="Background" Duration="0">
                                            <DiscreteObjectKeyFrame Value="{StaticResource Inverted}" KeyTime="0"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MainBorder" Storyboard.TargetProperty="BorderBrush" Duration="0">
                                            <DiscreteObjectKeyFrame Value="{StaticResource Custom}" KeyTime="0"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentElement" Storyboard.TargetProperty="Foreground" Duration="0">
                                            <DiscreteObjectKeyFrame Value="{StaticResource Custom}" KeyTime="0"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ActionIcon" Storyboard.TargetProperty="Source" Duration="0">
                                            <DiscreteObjectKeyFrame Value="/Images/cancel_dark.png" KeyTime="0"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Unfocused"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="MainBorder" BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}"
                        Margin="{StaticResource PhoneTouchTargetOverhang}">
                            <Grid>
                                <ContentControl x:Name="ContentElement" BorderThickness="0"
                                        HorizontalContentAlignment="Stretch"
                                        Margin="6,3,0,3"
                                        Padding="{TemplateBinding Padding}"
                                        VerticalContentAlignment="Stretch" 
                                        VerticalAlignment="Center"
                                        Opacity="{TemplateBinding Opacity}"/>
                                <Image Tap="{TemplateBinding ActionIcon}" x:Name="ActionIcon" HorizontalAlignment="Right" Height="54" Source="/Images/cancel_light.png" Width="54"/>
                            </Grid>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

Tap="{TemplateBinding ActionIcon}"错误,您将事件绑定到PhoneTextBox的ActionIcon属性。

如果您希望图像源为TemplateBinding

,则应该是这样的
Source="{TemplateBinding ActionIcon}"

要解决您的问题,您可以使用ActionIconTapped

toolkit:PhoneTextBox
 <toolkit:PhoneTextBox ActionIconTapped="PhoneTextBox_ActionIconTapped"/>