Binding images to an ImageButton at runtime
-
I'm finally getting a chance to work with WPF. Making a Button with an Image on it is no problem as long as I don't mind embedding the image in the resource. What can't get working is to have it load the image from file in a subfolder under the Application at runtime. I don't want it in the embedded resources. Any ideas? It must be possible to do.
Happy programming!!
-
I'm finally getting a chance to work with WPF. Making a Button with an Image on it is no problem as long as I don't mind embedding the image in the resource. What can't get working is to have it load the image from file in a subfolder under the Application at runtime. I don't want it in the embedded resources. Any ideas? It must be possible to do.
Happy programming!!
An easy way to do this is to specify a Uri to the image as in the following code:
myImage.BeginInit();
myImage.Source = new BitmapImage(new Uri(file));
myImage.EndInit();Obviously, you'll have to pass the fully qualified file name into the Uri, but this code will work.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
An easy way to do this is to specify a Uri to the image as in the following code:
myImage.BeginInit();
myImage.Source = new BitmapImage(new Uri(file));
myImage.EndInit();Obviously, you'll have to pass the fully qualified file name into the Uri, but this code will work.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
How does one go about putting it on the button? It must be possible but documentation on this topic worse than ever and impossible to find.
Happy programming!!
-
How does one go about putting it on the button? It must be possible but documentation on this topic worse than ever and impossible to find.
Happy programming!!
There same property you set in XAML, you can set in code.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
How does one go about putting it on the button? It must be possible but documentation on this topic worse than ever and impossible to find.
Happy programming!!
It's not that hard to do, and there are plenty of samples on the web. Your XAML looks like this:
<Button>
<Image x:Name="myImage" />
</Button>"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
It's not that hard to do, and there are plenty of samples on the web. Your XAML looks like this:
<Button>
<Image x:Name="myImage" />
</Button>"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
Thanks for your assistance. There are heaps of examples that load images from embedded resources for sure. So far I have found only one that half works the way I need it to work.
Happy programming!!
-
Thanks for your assistance. There are heaps of examples that load images from embedded resources for sure. So far I have found only one that half works the way I need it to work.
Happy programming!!
I'm glad I could help then.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.