Custom buttons in WP8.1 XAML apps

房东的猫 提交于 2020-03-26 05:41:08

问题


I'm trying to create a "hover" effect when the user touches their finger down on to a button. When that happens I would like to background color of the button to change - and then when they move it away it changes back to the original color.

My code for the whole button is below

<Button x:Name="btnService"  HorizontalAlignment="Center" Tag="{Binding Tag}" Command="{Binding DataContext.ConnectServiceCommand, ElementName=LayoutRoot}" CommandParameter="{Binding Tag}">
                            <Button.Template>
                                <ControlTemplate>

                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="350" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>

                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="64" />
                                            <RowDefinition Height="10" />
                                        </Grid.RowDefinitions>

                                        <Border IsTapEnabled="True" CornerRadius="0" BorderBrush="Transparent" BorderThickness="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Row="0" Grid.Column="1">

                                            <TextBlock Style="{StaticResource BodyTextBlockStyle}" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="{StaticResource accentForeground}" Text="{Binding NameUpper}" />

                                            <Border.Style>
                                                <Style TargetType="Border">
                                                    <Setter Property="Background" Value="{StaticResource bodyAccent}" />

                                                </Style>
                                            </Border.Style>
                                        </Border>

                                    </Grid>


                                </ControlTemplate>
                            </Button.Template>
                        </Button>

What I can't get working is the trigger as it doesn't seem to exist - I know normal WPF XAML apps have something like <Trigger Property="Border.IsMouseOver" Value="True">

Is there something like this for Windows Phone 8.1 apps?

Edit - Code added with VSManager

<Button x:Name="btnService"  HorizontalAlignment="Center" Tag="{Binding Tag}" Command="{Binding DataContext.ConnectServiceCommand, ElementName=LayoutRoot}" CommandParameter="{Binding Tag}">
                            <Button.Template>
                                <ControlTemplate>

                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="350" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>

                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="64" />
                                            <RowDefinition Height="10" />
                                        </Grid.RowDefinitions>

                                        <Border IsTapEnabled="True" CornerRadius="0" BorderBrush="Transparent" BorderThickness="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Row="0" Grid.Column="1">

                                            <TextBlock Style="{StaticResource BodyTextBlockStyle}" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="{StaticResource accentForeground}" Text="{Binding NameUpper}" />

                                            <Border.Style>
                                                <Style TargetType="Border">
                                                    <Setter Property="Background" Value="{StaticResource bodyAccent}" />

                                                </Style>

                                            </Border.Style>

                                        </Border>

                                    </Grid>


                                </ControlTemplate>
                            </Button.Template>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <PointerDownThemeAnimation Storyboard.TargetName="Grid"/>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Border">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource bodyAccentAlt}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                        </Button>

回答1:


In the default Button style, there are some predefined visual states. In those visual states, you can define what happens when the Button is pressed

<Style x:Key="ButtonStyle1" TargetType="Button">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="{ThemeResource PhoneForegroundBrush}"/>
    <Setter Property="Foreground" Value="{ThemeResource PhoneForegroundBrush}"/>
    <Setter Property="BorderThickness" Value="{ThemeResource PhoneBorderThickness}"/>
    <Setter Property="FontFamily" Value="{ThemeResource PhoneFontFamilyNormal}"/>
    <Setter Property="FontWeight" Value="{ThemeResource PhoneButtonFontWeight}"/>
    <Setter Property="FontSize" Value="{ThemeResource TextStyleLargeFontSize}"/>
    <Setter Property="Padding" Value="9.5,0"/>
    <Setter Property="MinHeight" Value="{ThemeResource PhoneButtonMinHeight}"/>
    <Setter Property="MinWidth" Value="{ThemeResource PhoneButtonMinWidth}"/>
    <Setter Property="HorizontalAlignment" Value="Left"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid x:Name="Grid" Background="Transparent">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualStateGroup.Transitions>
                                <VisualTransition From="Pressed" To="PointerOver">
                                    <Storyboard>
                                        <PointerUpThemeAnimation Storyboard.TargetName="Grid"/>
                                    </Storyboard>
                                </VisualTransition>
                                <VisualTransition From="PointerOver" To="Normal">
                                    <Storyboard>
                                        <PointerUpThemeAnimation Storyboard.TargetName="Grid"/>
                                    </Storyboard>
                                </VisualTransition>
                                <VisualTransition From="Pressed" To="Normal">
                                    <Storyboard>
                                        <PointerUpThemeAnimation Storyboard.TargetName="Grid"/>
                                    </Storyboard>
                                </VisualTransition>
                            </VisualStateGroup.Transitions>
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="PointerOver">
                            </VisualState>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <PointerDownThemeAnimation Storyboard.TargetName="Grid"/>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonPressedForegroundThemeBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Border">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="Yellow"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonDisabledForegroundThemeBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="Border">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonDisabledBorderThemeBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Border">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonDisabledBackgroundThemeBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Margin="{ThemeResource PhoneTouchTargetOverhang}">
                        <ContentPresenter x:Name="ContentPresenter" AutomationProperties.AccessibilityView="Raw" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

In

<VisualState x:Name="Pressed">

is what happens when the Button is pressed. You can easily define the background of the Border element

<VisualState x:Name="Pressed">
    <Storyboard>
        <PointerDownThemeAnimation Storyboard.TargetName="Grid"/>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonPressedForegroundThemeBrush}"/>
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Border">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource bodyAccent}"/>
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>

The important part is the ObjectAnimationUsingKeyFrames targeting Background property of the element with the name "Border"

<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Border">
    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource bodyAccent}"/>
</ObjectAnimationUsingKeyFrames>

And Button style is applied like this:

<Button Height="200" Style="{StaticResource ButtonStyle1}"/>


来源:https://stackoverflow.com/questions/23858332/custom-buttons-in-wp8-1-xaml-apps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!