Binding against Image source property - wpf

I need to binding to image.source property.
this works obviusly:
<Image Source="/Intecsal.Generico.Recursos;component/Images/Flags/ES.png"/>
but, I have a var with the string "ES" and I want to do something like this:
<Image Source="{Binding var, StringFormat=/Intecsal.Generico.Recursos;component/Images/Flags/{0}.png}"/>
But this doesn't work.. the image is not shown.
Can someone help me? thanks!

It doesnt work that way. Source property has the type ImageSource and every string being passed through Binding will be converter to new ImageSource instance. Binding has internally many converters.
That is the reason why your StringFormat has no effect at all and it never will :)
However, you still have the option to use Converter in Binding that will convert your properties value into whatever you wish :)
Take a look at this code line:
<Image Source="{Binding Path=Whatever,
Converter={StaticResource WhateverToSourceConverter}"/>

Related

bound picture to control in WPF without using converter

i'm having a custom object with Picture as a string (file name only)
and i want to display.
the problem is that the image is stored in "images" directory of the project so the full path is now "images/{image name}"
how can i bind it correctly ?
i can do it easily with converter i guess, but is there any way to avoid it like:
<Image Source="{Binding Path=FullPath+"/"+Picture}"> ?
You could bind to an aggregated property in your ViewModel and then put whatever path you like into that. That way the ViewModel becomes the 'converter'.

binding an image source in XAML

I am trying to bind an image source to my XAML through c#
this works
<Image Source="images/man.jpg"></Image>
this does not work
<Image Source="images/{Binding imagesource}"></Image>
where imagesource is a string variable in the c# file of this xaml and is being set equal to "man.jpg"
here is a way how to do it in XAML:
add this to the namespace:
xmlns:System="clr-namespace:System;assembly=mscorlib"
then add your images paths
<System:String x:Key="ImageRefresh">/Theme;component/Images/icon_refresh.png</System:String>
<System:String x:Key="ImageSearch">/Theme;component/Images/icon_search.png</System:String>
This is how you use it
<Image Height="16" Source="{StaticResource ImageSearch}" Stretch="Uniform" Width="16"/>
This works ok, but if you load your xaml style in Blend it will go bogus..
An object of type "System.String" cannot be applied to a property that expects the type "System.Windows.Media.ImageSource".
I haven't figured out yet, how to replace System:String with that Media.ImageSource... but hey.. it works for me in Visual Studio.
You can't stick a binding mid-way through the value like that. It's either a binding, or it's not. Assuming imagesource is publicly accessible via your DataContext, you could do this:
<Image Source="{Binding imagesource}"/>
However, if it's been set to "man.jpg" then it won't find the image. Either set imagesource to the full path ("images/man.jpg") or use a converter:
<Image Source="{Binding imagesource, Converter={StaticResource RelativePathConverter}}"/>
The converter would prepend "images/" onto its value. However, it may be necessary for the converter to return an ImageSource rather than a string.
Images have bitten me in the past. There is a certain lookup order involved.
When you use "image/man.jpg" it could refer to a file inside your silverlight xap, or relative to the location of XAP file. For example, it could be in YourProject.Web/ClientBin/image/man.jpg.
You should troubleshoot by using full URLs first and find out if this works.
imagesource needs to be an actual Image object, not a string.
Here is a method that will create a new Image object given a path:
public BitmapImage Load(string path)
{
var uri = new Uri(path);
return new BitmapImage(uri);
}

WPF textblock binding question

I'm trying to get my head around the whole MVVM thing and binding. I have a ViewModel class which has a property that is another class. I want to bind to a (string) property of that class to the text of a textblock.
I set the ViewModel as my data context for my window\page. And then do this:
<TextBlock Text="{Binding ElementName=myAddressClass, Path=StreetName}" />
But this does not work. The text is empty.
I can expose the StreetName directly as below and this works:
<TextBlock Text="{Binding Path=StreetName}" />
So am I doing something wrong in the first example. It seems simple enough ... am I just confuse about what an elementname is or should be set to?
thanks
I think you probably are confused. If you want to bind to MyAddress.StreetName, just do this: Text="{Binding MyAddress.StreetName}" Make sure MyAddress is a property of your DataContext. ElementName is for binding to other controls.
ElementName is used to reference a XAML element in the Logical Tree. Since what you're trying to bind to is not an element, ElementName isn't the correct approach. Dotted path notation is the simplest approach in this case:
{Binding Path=myAddressClass.StreetName}

WPF Update Binding when Bound directly to DataContext w/ Converter

Normally when you want a databound control to 'update,' you use the "PropertyChanged" event to signal to the interface that the data has changed behind the scenes.
For instance, you could have a textblock that is bound to the datacontext with a property "DisplayText"
<TextBlock Text="{Binding Path=DisplayText}"/>
From here, if the DataContext raises the PropertyChanged event with PropertyName "DisplayText," then this textblock's text should update (assuming you didn't change the Mode of the binding).
However, I have a more complicated binding that uses many properties off of the datacontext to determine the final look and feel of the control. To accomplish this, I bind directly to the datacontext and use a converter. In this case I am working with an image source.
<Image Source="{Binding Converter={StaticResource ImageConverter}}"/>
As you can see, I use a {Binding} with no path to bind directly to the datacontext, and I use an ImageConverter to select the image I'm looking for. But now I have no way (that I know of) to tell that binding to update. I tried raising the propertychanged event with "." as the propertyname, which did not work.
Is this possible? Do I have to wrap up the converting logic into a property that the binding can attach to, or is there a way to tell the binding to refresh (without explicitly refreshing the binding)?
Any help would be greatly appreciated.
Thanks!
-Adam
The workaround here was to add a property to my object (to be used as the datacontext) called "Self" , which simply returned
public Object Self { get { return this; }}
Then in the binding I used this property:
<Image Source="{Binding Path=Self, Converter={StaticResource ImageConverter}}"/>
Then when I call
PropertyChanged(this, new PropertyChangedEventArgs("Self"))
it works like a charm.
Thanks all.
I don't believe there is a way of accomplishing exactly what you need with your current converter. As you mentioned, you could do the calculation in your ViewModel, or you could change your converter into an IMulitValueConverter.
From your specific scenario (the converter tied to a ViewModel class, and a few of its properties), I would lean towards implementing the logic in the ViewModel.
Hmm, you don't show the full implementation. But I think it should update, if the value bound to the GUI provides the PropertyChanged-Event.
Regards

Get DynamicResource Binding in WPF

Can any one help me to get DynamicResource Binding in WPF by code?
I have set binding Like follow,
TextBlock Background={DynamicResource ColorA} Name="TB" in Xaml.
and i need to get the - TB's background is binded to "ColorA".
how can i access this DynamicResource Binding Expression in WPF by coding.
when i try to get TB.Background, it is white(#FFFFF..) or if i already given the value to the
Resorce key "ColorA" that will be given.
but i want to get this Binding Expression.
Thank in advance for your Help.
I think my Question wasn't clear.
I want to get What Reource Binding was done to the "TB" in Xaml by code.
But the aren't any TB.GetResourceReference. I Want some think like that.
Where that Binding expression is kept in WPF. I need to get the TB's BackgroundProperty was
Binded to Which( answer "ColorA") key?
thank a lot for sudden response.
You can use the FrameworkElement.SetResourceReference method:
MSDN: http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.setresourcereference.aspx
Provided your xaml has this:
<TextBlock x:Name="TB">
You can write this in the code behind:
TB.SetResourceReference(BackgroundProperty, "ColorA");
You can use this:
YourControl.Style = this.FindResource(NameOfYourStyleForThisControl) as Style;

Resources