views:

384

answers:

2

Hi all,

This is annoying - I know how I'd do it in web...

I have a wpf image and I want to bind it's source property to a method, and pass in a property from it's bound item.

IE.

<image source="GetMySource({binding name})" />

Where GetMySource is

protected string GetMySource(string name)
{
return "images/"+name+".png";
}
+1  A: 

This is the reason .NET does not use methods to return simple objects, but CLR properties. You're doing it the Java style, not the .NET style.

public string MySource {
  get { return "images/" + name + ".png"; }
}

Now the property is exposed, you have some choices :

  1. DataBind your view to itself (in constructor : DataContext = this;)

    < Image source="{Binding MySource}" />

  2. Name your UserControl (or whatever) (<UserControl x:name="this" ...>)

    < Image source="{Binding MySource, ElementName=this}" />

  3. Use relativesource binding

    < Image source="{Binding MySource, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}" />

EDIT :

If I understand well, actually you want to do a binding with a parameter (the name of the image here). You can't. Only Command bindings allows a CommandParameter. You could use many resource declared ObjectDataProviders, or Converters, but that's too much overhead for your simple job.

Your simple solution : use an array for all your images, and one-way bind to the individual objects of this array.

private string[] mysource = new[] {
  "images/foo.png", 
  "images/bar.png", 
  "images/baz.png"};
public string[] MySource {
  get { return mySource; }
}
  1. DataBind your view to itself (in constructor : DataContext = this;)

    < Image source="{Binding MySource[0]}" />

    1. ...etc.

I did not test it, but the man from the following article did : http://www.codeproject.com/Articles/37352/WPF-Binding-to-individual-collection-items-but-not.aspx

Aurélien Ribon
OK we're getting close so maybe I should clarify my requirement :)If I use CPR property as shown in last post, my image lives inside a listview which has been bound using itemssouce. Therefore "Name" is coming from the binding applied to the listview.THe image is inside the listview and I need to now say "Source="{Binding MySource(Name)}" - or whatever appropriate command you give me :)
Matt B
If the source of the image depends on the item, then the Property probably goes in class that represents each item.
Scott J
The name of the item is in there, I need the root source to be flexible. This is the most simple thing in the world with ASP.NET, why does WPF have to be so damned difficult.
Matt B
A: 

Never mind, I solved it another way for now. I'm sure this is something I'll need to do at some point though and I don't really want to use a convertor so if anyone has an answer that would be good.

Matt B
Could you mark Aurelien as correct or post your answer please to help others looking for a similar answer :)
Chris
But none of them gave me the answer I needed, I'm leaving it open in case anyone knows the right answer.
Matt B
I edited my solution to give you a possible simple solution.
Aurélien Ribon