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>

和我的cs:

    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'。谁能帮我解释一下,谢谢

4

3 回答 3

15

在构建操作中,您可以将图像文件标记为内容或资源。在 ImageBrush 中使用图像的语法因您选择的语法而异。

这是一个标记为内容的图像文件。

图片,标记为内容

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

 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;
于 2013-04-09T05:52:35.673 回答
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>
于 2013-08-08T11:53:45.530 回答
1

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

设置构建资源的操作

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

于 2013-04-09T02:47:26.203 回答