c#-更改悬停按钮的图像背景并单击
我添加了一个按钮,然后将其背景更改为图片。。当它运行时,如果我将鼠标悬停在按钮上,图片将消失,当我单击它时,它将变成纯白色 我想改变这一点。。我想在鼠标悬停并单击时显示图片 怎么做??请帮帮我 im使用visual studio 2013-windows应用商店-c#/xaml 这是我的btton的xaml代码c#-更改悬停按钮的图像背景并单击,c#,xaml,windows-8,windows-store-apps,C#,Xaml,Windows 8,Windows Store Apps,我添加了一个按钮,然后将其背景更改为图片。。当它运行时,如果我将鼠标悬停在按钮上,图片将消失,当我单击它时,它将变成纯白色 我想改变这一点。。我想在鼠标悬停并单击时显示图片 怎么做??请帮帮我 im使用visual studio 2013-windows应用商店-c#/xaml 这是我的btton的xaml代码 <Button Content="Asia" HorizontalAlignment="Left" Margin="232,366,0,0" VerticalAlignment=
<Button Content="Asia" HorizontalAlignment="Left" Margin="232,366,0,0" VerticalAlignment="Top" Height="395" Width="235" BorderThickness="0">
<Button.Background>
<ImageBrush ImageSource="south amirica.png"/>
</Button.Background>
</Button>
我是否在此处更改了某些内容???您描述的行为是
按钮的默认模板的一部分。要自定义此行为,您可以定义自定义的按钮。模板:
<Button Content="Asia" HorizontalAlignment="Left" Margin="232,366,0,0" VerticalAlignment="Top" Height="395" Width="235">
<Button.Template>
<ControlTemplate TargetType="Button">
<Grid>
<Grid.Background>
<ImageBrush ImageSource="south amirica.png"/>
</Grid.Background>
<ContentPresenter/>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
请注意,这也将删除所有其他效果,例如按钮按下效果,但如果需要,您可以将其添加到模板中
<Button Height="35" Width="200">
<Button.Template>
<ControlTemplate TargetType="Button">
<Grid>
<Grid.Resources>
<BitmapImage x:Key="NormalButton" UriSource="Assets/NormalButton.png"></BitmapImage>
<BitmapImage x:Key="OnMouseOver" UriSource="Assets/OnMouseOver.png"></BitmapImage>
<BitmapImage x:Key="OnPresed" UriSource="Assets/OnPresed.png"></BitmapImage>
</Grid.Resources>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="PointerOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="ImageSource" Storyboard.TargetName="Border">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource OnMouseOver}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="ImageSource" Storyboard.TargetName="Border">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource OnPresed}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid>
<Grid.Background>
<ImageBrush x:Name="Border" ImageSource="{StaticResource NormalButton}"></ImageBrush>
</Grid.Background>
<ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
您能告诉我如何将按钮单击添加到我的模板中吗?您能帮助我吗?我刚刚意识到我不需要单击效果!!非常感谢您提供的简单实用的解决方案!!!没问题@Lamatawany。很高兴这有帮助。至于按钮按下的效果,我没有包括它,因为它将取决于你想要什么效果。您可以设置颜色动画、翻译按钮、更改边框厚度等。我编辑了我的应用程序,它可以正常运行,但如果我单击它,它将中断:(为什么?上面的示例在这里工作正常。抱歉,我不知道您的情况下出现了什么问题。错误是什么消息?thanx。