XAML: how to access items from resources.resx [duplicate] - wpf

This question already has answers here:
How to get a Uri of the image stored in the resources
(2 answers)
Assign BitmapImage from Resources.resx to Image.Source?
(1 answer)
Not able to reference Image source with relative path in xaml
(3 answers)
Closed 3 years ago.
I'm learing WPF and XAML. I'm trying to show an image loaded from resources.resx.
For this, I've added an existing image to resources.resx. The identifier of the image is GoogleLogo. If I open Resources.ResX is can see that the image indeed is present in the resource file.
Now I want a window that shows this image in a Grid. The XAML is like:
<Window x:Class="WpfDemo.MainWindow"
xmlns=...
Title="Show Google Logo" Height="160" Width="800">
<Grid>
<Image Source="???"/>
</Grid>
</Window>
How to refer to the Source in resources.ResX? What to type instead of ???

Related

Set source of wpf image [duplicate]

This question already has answers here:
How to load image to WPF in runtime?
(2 answers)
WPF - Import image as resource
(3 answers)
Closed 1 year ago.
I have a wpf app with an image in the xaml:
<Image HorizontalAlignment="Center" Height="131" Margin="550,0,1306,-960" VerticalAlignment="Bottom" Width="58" Source="../images/blue.JPG" Name="im_Izq"/>
And I want to change the source image from the code behind.
I tried the following:
im_Izq.Source = new BitmapImage(new Uri("../../images/red.JPG"));
but, when running it, I get this error:
System.UriFormatException: 'URI no vĂ¡lido: no se puede determinar el formato del URI.'
Can you help me? Thanks!!
Make sure the image file is located in a project folder named images, and set the Build Action of the file to Resource.
im_Izq.Source = new BitmapImage(new Uri("pack://application:,,,/images/red.JPG"));
or use
im_Izq.Source = new BitmapImage(new Uri(#"/projectname;component/Images/up.ico", UriKind.Relative));

Use StringFormat for special output in XAML-Binding [duplicate]

This question already has answers here:
StringFormat is ignored
(3 answers)
Closed 6 years ago.
I have an integer value named Id and want a Label in Xaml to display the following:
(ID: 160)
I tried the following:
<Label Content="{Binding Id, StringFormat='(ID: {0:0})'} />
but it doesn't work - it just displays the value of Id:
160
How can I get this working without using a special ValueConverter class?
You have to use ContentStringFormat for content controls instead of the binding's StringFormat, has been asked various times before, will have to look for that.

how to show image along with conent on a button in wpf using mvvm [duplicate]

This question already has answers here:
Button template with image and text in wpf
(7 answers)
Closed 8 years ago.
I am creating one sample WPF-MVVM application , in that I have one image which indicates '+' sign and I have a button with content 'Edit'. Now I have to show the image along with Name 'Edit' on a button. Please let me know the solution for this problem.
EDIT
here I am able to show the image but the name is appered below the image. But I want to show the name beside the image.
you can do something like this
<Button Margin="104,78,84,60" Name="button1" Height="100" Width="200">
<StackPanel Orientation="Horizontal">
<Image Source="ssv.png" Stretch="None" Height="50" Width="50" />
<TextBlock TextAlignment="Center">Text value for this.</TextBlock>
</StackPanel>
</Button>
There are a few ways you could create an 'image button', one way would be to create a custom which derives from Button and adds an Image property, or a user control that uses a Button and declaratively adds the image as part of the buttons's control template in the user control XAML.
However, these approaches mean that the layout of the image within the button is fixed, so it isn't particularly flexible.
A nicer option is to create an attached property which stores the image location, and then reference this attached property value in the buttons control template. You can then create a style for the control template to make the layout reusable across buttons.

Image shows in Expression Blend but not during runtime

The image in question is located in a ControlTemplate inside of a ResourceDictionary similar to this (various details removed for clarity):
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
<ControlTemplate x:Key="ImageTestTemplate" TargetType="ImageTest">
<Grid>
<Image Source="/MyAssembly;Component/Images/MyImage.png"/>
</Grid>
</ControlTemplate>
</ResourceDictionary>
When I view the control in Expression Blend the image shows up just fine, but when I run the app the image does not show up. If I drop the same image into a UserControl it also shows up just fine, so the problem has something to do with using the template.
Is there some other way I should be loading the image resource inside the template?
I don't think you need the leading '/'. The base resource path I'm using for a project is "IMLPrototype2;component/Model/Sounds/" in SL 3 & 4.

Binding images to Image in Silverlight 2

I'm building a Silverlight 2 application and I need to bind some images to Image object declarated in XAML. I'm doing some other binding in the application and it works just fine, I'm having problem with only images!
This is it's XAML:
<Image Source="{Binding Path=ThumbNail}" Style="{StaticResource ThumbNailPreview}" />
And this is the C# code-behind:
ThumbNail = (string)story.Element("thumbnail").Attribute("src")
I'm just parsing the URL's from a XAML file. When I try to do a foreach loop over all of the ThumbNail properties, it returns the URL as expected. As I said, all other binding works great, so it has to be a problem between the Image control and the ThumbNail property. I also tried chanding the datatype from string to Uri, but nothing happened. Could anyone help me?
I think this has already been asked and answered in StackOverflow here
The key is that the ImageSource property is not a string but a System.Windows.Media.Imaging.BitmapImage type and so needs to be converted.
HTH

Resources