带图像的WPF按钮

won*_*nea 102 wpf

我正在尝试在WPF中的按钮上附加图像,但此代码失败.在类似的代码在Mozilla XUL中完美运行后似乎很奇怪.

<Button Height="49.086" Margin="3.636,12,231.795,0" Name="button2" 
        VerticalAlignment="Top" Grid.Column="1" Click="button2_Click" 
        Source="Pictures/apple.jpg">Disconnect from Server</Button>
Run Code Online (Sandbox Code Playgroud)

wpf*_*abe 221

你想做这样的事情:

<Button>
    <StackPanel>
        <Image Source="Pictures/apple.jpg" />
        <TextBlock>Disconnect from Server</TextBlock>
    </StackPanel>
</Button>
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记将apple.jpg添加到项目中.(构建行动:资源) (3认同)
  • 这是一个非常糟糕的解决方案,因为当您禁用按钮时,文本和图像不会被__去掉. (3认同)
  • @NumLock您的解决方案在哪里? (3认同)

Sai*_*Sai 11

另一种将图像拉伸到完整按钮的方法.可以尝试下面的代码.

<Grid.Resources>
  <ImageBrush x:Key="AddButtonImageBrush" ImageSource="/Demoapp;component/Resources/AddButton.png" Stretch="UniformToFill"/>
</Grid.Resources>

<Button Content="Load Inventory 1" Background="{StaticResource AddButtonImageBrush}"/> 
Run Code Online (Sandbox Code Playgroud)

这里引用

它也可能有助于其他人.我在这里发布了与MouseOver Option相同的内容.


Sha*_*gwu 11

最简单的方法是使用图像标签。

<Button Name="btn" Width="26" Height="26" Click="btnClick"> 
   <Image Source="Resource/btn-icon.png"/>
</Button>
Run Code Online (Sandbox Code Playgroud)

假设您的图像文件添加到资源文件夹中


小智 5

<Button x:Name="myBtn_DetailsTab_Save" FlowDirection="LeftToRight"  HorizontalAlignment="Left" Margin="835,544,0,0" VerticalAlignment="Top"  Width="143" Height="53" BorderBrush="#FF0F6287" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontFamily="B Titr" FontSize="15" FontWeight="Bold" BorderThickness="2" Click="myBtn_DetailsTab_Save_Click">\n    <StackPanel HorizontalAlignment="Stretch" Background="#FF1FB3F5" Cursor="Hand" >\n        <Image HorizontalAlignment="Left"  Source="image/bg/Save.png" Height="36" Width="124" />\n        <TextBlock HorizontalAlignment="Center" Width="84" Height="22" VerticalAlignment="Top" Margin="0,-31,-58,0" Text="\xd8\xab\xd8\xa8\xd8\xaa \xd9\x85\xd8\xb4\xd8\xaa\xd8\xb1\xdb\x8c" />\n    </StackPanel>\n</Button>\n
Run Code Online (Sandbox Code Playgroud)\n