PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, May 6, 2022

[FIXED] How do I bind an Image Source?

 May 06, 2022     binding, c#, expression-blend, image     No comments   

Issue

Here is what I have so far:

<Image Source="{Binding ImageSource"} />

<Button Content"Text" ImageSource="path/image.png" />

I know something isn't right here. I guess I can't see where ImageSource is defined.

I have several of these buttons and just want to have a unique image for each one. I have a button template that I am using and it works great for the text.

<Label Content="TemplateBinding Content" />

Thanks for all your help!


Solution

In your case, it can be very easy!

Add the images as resources to your project, then in XAML use something like the following:

<Button HorizontalAlignment="Left" Margin="20,0,0,20" VerticalAlignment="Bottom" Width="50" Height="25">
    <Image Source="image.png" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0">
    </Image>
</Button>

Or, the more complicated way:

If you use the MVVM Pattern, you can do the following

In your XAML:

<Button Focusable="False" Command="{Binding CmdClick}" Margin="0">
    <Image Source="{Binding ButtonImage}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0">
    </Image>
</Button>

In your ViewModel:

private Image buttonImage;

public Image ButtonImage 
{
    get
    {
       return buttonImage;
    }
}

And somewhere in the constructor of your ViewModel or the initialisation of it:

BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri("image.png", UriKind.Relative);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();

buttonImage = new Image();
buttonImage.Source = src;


Answered By - Mare Infinitus
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing