Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WPF按钮中添加图像_C#_Wpf_Image_Button - Fatal编程技术网

C# 在WPF按钮中添加图像

C# 在WPF按钮中添加图像,c#,wpf,image,button,C#,Wpf,Image,Button,我尝试了这个解决方案: <Button> <StackPanel> <Image Source="Pictures/img.jpg" /> <TextBlock>Blablabla</TextBlock> </StackPanel> </Button> 我在PresentationFramework.dll中得到一个“'System.Windows.Markup.

我尝试了这个解决方案:

<Button>
    <StackPanel>
        <Image Source="Pictures/img.jpg" />
        <TextBlock>Blablabla</TextBlock>
    </StackPanel>
</Button>
我在PresentationFramework.dll中得到一个“'System.Windows.Markup.XamlParseException”异常


解决方案是什么?

请尝试以下XAML代码片段:

<Button Width="300" Height="50">
  <StackPanel Orientation="Horizontal">
    <Image Source="Pictures/img.jpg" Width="20" Height="20"/>
    <TextBlock Text="Blablabla" VerticalAlignment="Center" />
  </StackPanel>
</Button>
使用:



它应该会起作用。但请记住,您必须将图像添加到项目的资源中

在“丢失”图像的情况下,需要考虑以下几点:

  • 当XAML找不到资源时,它可能会忽略它(当它不会抛出
    XamlParseException

  • 必须正确添加和定义资源:

    • 确保它存在于项目中预期的位置。

    • 确保它是以您的项目作为资源构建的。

      (右键单击→ 性质→ BuildAction='Resource')

  • 在类似情况下还需要尝试另一件事,这对于重用映像(或任何其他资源)也很有用:

    将图像定义为XAML中的资源:

    
    
    然后在所需控件中使用它:


    如果要覆盖文本,可以将按钮的背景设置为图像

    <Button>
       <Button.Background>
         <ImageBrush ImageSource="/AssemblyName;component/Pictures/img.jpg"/>
       </Button.Background>
       <TextBlock>Blablabla</TextBlock>
    </Button>
    
    
    喋喋不休
    
    注意图像源语法。请参阅以获取帮助。

    尝试内容模板:

    <Button Grid.Row="2" Grid.Column="0" Width="20" Height="20"
            Template="{StaticResource SomeTemplate}">
        <Button.ContentTemplate>
            <DataTemplate>
                <Image Source="../Folder1/Img1.png" Width="20" />
            </DataTemplate>
        </Button.ContentTemplate>
    </Button>
    

    我也有同样的问题。我已经用下面的代码修复了它

            <Button Width="30" Margin="0,5" HorizontalAlignment="Stretch"  Click="OnSearch" >
                <DockPanel>
                    <Image Source="../Resources/Back.jpg"/>
                </DockPanel>
            </Button>
    
    
    
    注意:确保属性窗口中图像的生成操作应为
    Resource


    我发现我还必须将设置为“公共”-默认设置为“内部”,我的图标仅在设计模式下出现,但在运行应用程序时不出现。

    您的图像在其属性中是否定义为“资源”?(右键单击它->属性->构建操作class='Resource')谢谢!您将我放在了正确的方向上:我在解决方案资源管理器中拖放了该图像,现在我可以看到它:)我认为您必须使用双引号,例如而不是content={…}。源路径可以尝试。我尝试了第二种方法,但遇到了此错误。“在类型‘UserControl’中找不到可附加属性‘Resources’。”我应该为sometemplate填写什么?如果您没有使用
    Template
    ,可以删除
    Template=“{StaticResource sometemplate}”
    ,如果您想知道模板是什么,可以尝试用谷歌
    wpf Template
    <Button>
       <Button.Background>
         <ImageBrush ImageSource="/AssemblyName;component/Pictures/img.jpg"/>
       </Button.Background>
       <TextBlock>Blablabla</TextBlock>
    </Button>
    
    <Button Grid.Row="2" Grid.Column="0" Width="20" Height="20"
            Template="{StaticResource SomeTemplate}">
        <Button.ContentTemplate>
            <DataTemplate>
                <Image Source="../Folder1/Img1.png" Width="20" />
            </DataTemplate>
        </Button.ContentTemplate>
    </Button>
    
            <Button Width="30" Margin="0,5" HorizontalAlignment="Stretch"  Click="OnSearch" >
                <DockPanel>
                    <Image Source="../Resources/Back.jpg"/>
                </DockPanel>
            </Button>