我有一个按钮,我正在尝试使用某些图像。该按钮的Xaml位于下方。
<Button Name="Button1" Grid.Row="1" Grid.Column="1" Background="Transparent" BorderBrush="Transparent" Click="Button1_Click" MouseEnter="Button1_MouseEnter" MouseLeave="Button1_MouseLeave">
<Image Name="Button1_Image" Source="/Assets/Button1Passive.png"/>
</Button>
背后的代码:
private void Button1_Click(object sender, RoutedEventArgs e)
{
if (!Button1Active)
{
BitmapImage Image1 = new BitmapImage(new Uri("/Assets/Button1Selected.png", UriKind.Relative));
Button1_Image.Source = Image1;
Button1Active = true;
}
else
{
BitmapImage Image1 = new BitmapImage(new Uri("/Assets/Button1Passive.png", UriKind.Relative));
Button1_Image.Source = Image1;
Button1Active = false;
}
}
private void Button1_MouseEnter(object sender, MouseEventArgs e)
{
if (!Button1Active)
{
BitmapImage Image1 = new BitmapImage(new Uri("/Assets/Button1RolledOver.png", UriKind.Relative));
Button1_Image.Source = Image1;
}
}
private void Button1_MouseLeave(object sender, MouseEventArgs e)
{
if (!Button1Active)
{
BitmapImage Image1 = new BitmapImage(new Uri("/Assets/Button1Passive.png", UriKind.Relative));
Button1_Image.Source = Image1;
}
}
我遇到的问题是图像没有占据整个按钮,当鼠标滑过图像时,图像周围有一个蓝色的框。
我已经查看是否有其他解决方案,我能找到的所有内容都不允许我以编程方式更改图像。
提前致谢。
答案 0 :(得分:3)
您应该覆盖Button
的模板
<Button Name="Button1" Grid.Row="1" Grid.Column="1"
Background="Transparent" BorderBrush="Transparent"
Click="Button1_Click" MouseEnter="Button1_MouseEnter"
MouseLeave="Button1_MouseLeave">
<Button.Template>
<ControlTemplate TargetType="Button">
<ContentPresenter Content="{TemplateBinding Content}"/>
</ControlTemplate>
</Button.Template>
<Image Name="Button1_Image" Source="/Assets/Button1Passive.png"/>
</Button>