locked
windows store app binding Button background to an image RRS feed

  • Question

  • Hello,

    My App using C# needs to dynamically create a bunch of clickable images.

    I tried:

    <Button Content="" Margin="20,0" Width="40" Height="30">
                                    <Button.Background>
                                        <ImageBrush ImageSource="{Binding Image}" Stretch="None"/>
                                    </Button.Background>
                                </Button>

    But didn't work.

    I tried some static string as <ImageBrush ImageSource="ms-appx:///Images/test.png" />, but didn't work either.

    How to make it work? Thanks.

    Wednesday, August 7, 2013 12:21 AM

Answers

All replies

  • Your XAML looks good, can you share the value of the Image string (the value you are binding the ImageSource to)? Also try <ImageBrush ImageSource="Images/test.png"/> and see if that works.

    Good luck,

    Hassan

    Wednesday, August 7, 2013 4:57 AM
  • Hi,Chunmiao

    I do not know what the specific value in your image.So I try set imageSource in XAML,I try like this:

    <Button Height="51" Margin="0,375,0,342" Width="68">
                <Button.Background>
                    <ImageBrush ImageSource="ms-appx:///Assets/Logo.png"></ImageBrush>
                </Button.Background>
            </Button>

    I found that code work ok.

    ms-appx  is used to refer to app files that come from the app's package. So,you can make sure what the specific position does the test.png exist.Then I recomment you can see this link to know kind of Uri schemes and how to set image source:

    http://msdn.microsoft.com/en-us/library/windows/apps/jj655406.aspx

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.image.aspx

    If there is something unclear, we are willing to help!

    Best Wishes!

    • Marked as answer by Chunmiao Wednesday, August 7, 2013 9:10 PM
    Wednesday, August 7, 2013 1:37 PM
  • Thanks you so much!

    I must have a typo or something, the image is showing indeed.

    Wednesday, August 7, 2013 9:09 PM