简体   繁体   中英

C# XAML (WPF) add button to image

I Have this code:

<Button >
      <Button.Content>
            <Image Source="component/Resources/Homer.jpg" />
      </Button.Content>
</Button>

This create an image as part of a button But i want to do the opposite thing, i want to have an image and make a button a part od that image

something like:

<Image >
     <Button>
     </Button>
</Image>

Is it possible? if yes, how?

Thanks you

This is not directly possible.

The reason is that the Image Class is designed to display an image, not host controls, and thus does not implement a content or inherit from ContentControl the way other hosting objects do.

While there no little reason I can fathom NOT to place an Image in a Button, there are a few options.

  • While Image does not implement a Click event, you can use Preview Mouse Down for a similar effect.
  • You can place both a Button and an Image in a Grid, causing them to share the same space
 <Grid > <Button ... HorizontalAlignment="Stretch" VerticalAlignment="Stretch" /> <Image ... /> </Grid > 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM