Windows phone 8.1 在Windows Phone 8.1中更改按下按钮时的不透明度
我有一个Windows Phone 8.1 RT应用程序 我的页面上有一个图像按钮 我想要的是,当我按下这个按钮时,我要么将边框设置为黄色,要么/并更改图像 我正在努力解决如何在故事板中设置这一点 这是我的标记(我删除了与网格无关的内容):Windows phone 8.1 在Windows Phone 8.1中更改按下按钮时的不透明度,windows-phone-8.1,Windows Phone 8.1,我有一个Windows Phone 8.1 RT应用程序 我的页面上有一个图像按钮 我想要的是,当我按下这个按钮时,我要么将边框设置为黄色,要么/并更改图像 我正在努力解决如何在故事板中设置这一点 这是我的标记(我删除了与网格无关的内容): 还有我的按钮: <Button BorderThickness="0" BorderBrush="Black" Name="btnDel" Grid.Row="4" Grid.Column="2" Width="75" Height
还有我的按钮:
<Button BorderThickness="0" BorderBrush="Black" Name="btnDel" Grid.Row="4" Grid.Column="2" Width="75" Height="75" HorizontalAlignment="Center" >
<Button.Background>
<ImageBrush ImageSource="ms-appx:///Images/del.png" Stretch="Uniform"/>
</Button.Background>
</Button>
谢谢您需要在
控制模板中命名要设置动画的元素,然后在情节提要中命名。TargetName
属性(这里我使用根网格
,创造性地命名为根):
MainPage.Xaml
<Page
x:Class="StyleTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:StyleTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.Resources>
<Style TargetType="local:MyButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:MyButton">
<Grid x:Name="Root" Background="Transparent" Margin="0,9.6">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="PointerOver"/>
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="Image" Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="Image" Background="{TemplateBinding PressedContent}" Visibility="Collapsed"/>
<Border x:Name="Border" BorderBrush="White" BorderThickness="2.4">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>
<StackPanel>
<local:MyButton Content="change bg!">
<local:MyButton.PressedContent>
<ImageBrush ImageSource="/Assets/Logo.png"/>
</local:MyButton.PressedContent>
</local:MyButton>
</StackPanel>
</Page>
这里的关键是MyButton
控件有一个名为PressedContent
的新属性,ControlTemplate
使用TemplateBinding
将其添加到按钮的视觉外观中。动画只需打开和关闭可见性。您有“Storyboard.TargetName=”Opacity“,这是一个错误,因为您没有任何名为“Opacity”的元素——您需要在控件模板中定位具有x:NameHI的对象,感谢您提供的信息。愚蠢的错误lol。如果我想用这种风格来定位我在这个页面上的所有按钮,那么我假设我没有设置TargetName属性?如果这是正确的,那么我已经删除了它,但我仍然得到一个错误…哇,多么全面的答案!谢谢
<Page.Resources>
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="Root" Background="Transparent" Margin="0,12">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="PointerOver"/>
<VisualState x:Name="Pressed">
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Root" To="0.3" Duration="0"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="Border" BorderBrush="White" BorderThickness="3">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>
<StackPanel>
<Button Content="hello"/>
<Button Content="there"/>
<Button Content="matey"/>
</StackPanel>
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
namespace StyleTest
{
public class MyButton : Button
{
public static readonly DependencyProperty PressedContentProperty = DependencyProperty.Register("PressedContent", typeof(Brush), typeof(MyButton), null);
public Brush PressedContent
{
get { return (Brush)GetValue(PressedContentProperty); }
set { SetValue(PressedContentProperty, value); }
}
}
}
<Page
x:Class="StyleTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:StyleTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.Resources>
<Style TargetType="local:MyButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:MyButton">
<Grid x:Name="Root" Background="Transparent" Margin="0,9.6">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="PointerOver"/>
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="Image" Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="Image" Background="{TemplateBinding PressedContent}" Visibility="Collapsed"/>
<Border x:Name="Border" BorderBrush="White" BorderThickness="2.4">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>
<StackPanel>
<local:MyButton Content="change bg!">
<local:MyButton.PressedContent>
<ImageBrush ImageSource="/Assets/Logo.png"/>
</local:MyButton.PressedContent>
</local:MyButton>
</StackPanel>
</Page>