如何在C# WPF代码中更改/设置按钮的背景图像?

9

我将尝试更改按钮的背景图像为其他图像,但是遇到了一些错误。 这是我的XAML代码:

    <Button x:Name="Button1" Width="200" Height="200" Content="Button1" Margin="0,0,0,400">
        <Button.Background>
            <ImageBrush **ImageSource ="Images/AERO.png"**  ></ImageBrush>
        </Button.Background>
    </Button>

我的计算机科学:

    private void Button1_Click_1(object sender, RoutedEventArgs e)
    {
        var brush = new ImageBrush();
        brush.ImageSource = new BitmapImage(new Uri("Images/AERO.png"));
        Button1.Background = brush;
    }

我在xaml中遇到的错误是“文件'Images\logo.png'不是该项目的一部分,或者它的'Build Action'属性未设置为'Resource'。请问有谁能帮我解释一下吗?谢谢。

2
请确认在Visual Studio中您的项目中是否包含文件“Image\logo.png”。如果是,请确保在其属性选项卡上将其构建操作设置为资源。 - Kohanz
我该如何设置“生成操作”属性?我似乎找不到它。 - Jaz
右键单击文件,选择属性。 - Kohanz
哪个文件?如果是图片文件,它已经设置为资源。 - Jaz
是的,我指的是图片文件(logo.png)。你清理并重新构建了吗? - Kohanz
1
一些令人困惑的方面:你的代码中写着"aero.png",但错误显示是"logo.png"。另外,在你的XAML中的 "**" 是什么意思?我猜那是强调的意思,因为它不能编译。 - Kohanz
3个回答

15
在构建操作中,您可以将图像文件标记为内容或资源。根据您选择的是哪种方式,使用ImageBrush中的图像的语法不同。
这里有一个被标记为内容的图像文件。 Image, marked as content 要将按钮背景设置为此图像,请使用以下代码。
 var brush = new ImageBrush();
 brush.ImageSource = new BitmapImage(new Uri("Images/ContentImage.png",UriKind.Relative));
 button1.Background = brush;

这是一个标记为资源的图片文件。

标记为资源的图片

要将按钮背景设置为资源图像,请使用以下代码。

  Uri resourceUri = new Uri("Images/ResourceImage.png", UriKind.Relative);
  StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri);

  BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream);
  var brush = new ImageBrush();
  brush.ImageSource = temp;

  button1.Background = brush;

我只有一个问题。找不到类型或命名空间名称“StreamResourceInfo”。(您是否缺少使用指令或程序集引用) - Jaz
这是一个常见的C#编译器错误。您需要在代码窗口顶部添加using System.Windows.Resources;。 - Walt Ritscher

3

以下是一个代码片段,将代码片段中提到的样式分配给按钮或切换按钮,然后您可以完全通过XAML控制更改的背景...不需要其他编码。我没有提供所有的代码,只是试着理解它的逻辑 :)

<Style x:Key="KeyStopButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border TextBlock.Foreground="{TemplateBinding Foreground}"
                                  x:Name="Border"
                                  CornerRadius="1"
                                  BorderThickness="1">
                    <Border.Background>                            
                        <ImageBrush ImageSource= "..\Images\ButtonImages\StopButton.png"  Stretch="Uniform"/>
                    </Border.Background>                        
                    <Border.Effect>                            
                        <DropShadowEffect/>                                
                    </Border.Effect>
                </Border>
                <ControlTemplate.Triggers>                        
                    <Trigger Property="IsPressed" Value="true">
                        <Setter TargetName="Border" Property="Border.Effect" Value="{x:Null}"/>                                                            
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="KeyPlayPauseButtonStyle" TargetType="{x:Type ToggleButton}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToggleButton">
                <Border TextBlock.Foreground="{TemplateBinding Foreground}"
                                  x:Name="Border"
                                  CornerRadius="1"
                                  BorderThickness="1">
                    <Border.Background>
                        <ImageBrush ImageSource= "..\Images\ButtonImages\PlayButton.png"  Stretch="Uniform"/>
                    </Border.Background>
                    <Border.Effect>
                        <DropShadowEffect/>
                    </Border.Effect>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="true">
                        <Setter TargetName="Border" Property="Border.Background">
                            <Setter.Value>
                                <ImageBrush ImageSource= "..\Images\ButtonImages\PauseButton.png" Stretch="Uniform"/>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

1
如果没有包含它,请在项目中包含文件“Image\Logo.png”。然后通过访问该文件的属性选项卡(右键单击),将其构建操作设置为“资源”。

Setting action to build resource

此外,我不确定您在按钮的 Click 处理程序中想要做什么。您已经在 XAML 中设置了背景图像。除非您在 Click 处理程序中将其设置为另一张图片,否则该代码不需要。

是的,它已经在资源下面了... 但现在没有错误,但我再也看不到主窗口上的按钮了。 - Jaz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接