views:

60

answers:

1

Im attempting to bind to the output of a method. Now I've seen examples of this using ObjectDataProvider However the problem with this is ObjectDataProvider creates a new instance of the object to call the method. Where I need the method called on the current object instance. I'm currently trying to get a converter to work.

Setup:

Class Entity
{
   private Dictionary<String, Object> properties;

   public object getProperty(string property)
  {
      //error checking and what not performed here
     return this.properties[property];
  }
}

My attempt at the XAML

     <local:PropertyConverter x:Key="myPropertyConverter"/>
      <TextBlock Name="textBox2">
          <TextBlock.Text>
            <MultiBinding Converter="{StaticResource myPropertyConverter}"
                          ConverterParameter="Image" >
              <Binding Path="RelativeSource.Self" /> <!--this doesnt work-->
            </MultiBinding>
          </TextBlock.Text>
        </TextBlock>

my code behind

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    string param = (string)parameter;
    var methodInfo = values[0].GetType().GetMethod("getProperty", new Type[0]);
    if (methodInfo == null)
        return null;
    return methodInfo.Invoke(values[0], new string[] { param });               
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
    throw new NotSupportedException("PropertyConverter can only be used for one way conversion.");
}

My problem is that I cant seem to pass the current Entity into the converter. So When i try to use reflection to get the getProperty method I have nothing to operate on

thanks, steph

+1  A: 

Wrap the call to the method inside a get property and add this get property to whatever class that is your current DataContext.

Edit: Answering your updated question.

If you only pass one parameter to the valueconverter you don't need a multivalueconverter, just use a regular valueconverter (implementing IValueConverter). Also, why not cast the object in the valueconverter to a Distionary and use it directly instead of using reflection.

To pass current datacontext as a binding do this: <Binding . />. I'm guessing the datacontext of the textblock is entity.

Still, all this is not necessary if all you want to do is run some code before accessing a dictionary item. Just use an index property instead, you can databind to it directly:

public class Entity 
{ 
   private Dictionary<String, Object> properties; 

   public object this[string property]
   {
        get
        { 
            //error checking and what not performed here 
            return properties[property]; 
        }
    } 
} 

<TextBlock Text="{Binding Path=[Image]}" />
Wallstreet Programmer
The problem with this is the dictionary of properties can contain MANY things. i dont want to write a get/set property for each potential property
Without Me It Just Aweso
You can databind to dictionary items without using a method. <TextBlock Text="{Binding Path=[MyKey]}" />
Wallstreet Programmer
Well attaching directly to the dictionary works but im hesitant to leave it publically exposed. I like having the error checking in the method. Is there no way to call the method to get the property? Also down the line i stil need to figure out how to call methods with dynamic propertyindex's. Perhaps controled by a dropdown/etc
Without Me It Just Aweso
Use a multivalueconverter which takes the dictionary and a key.
Wallstreet Programmer
Hey thanks you SOO much im getting closer but still havn't gotten it quite yet... I have set up a multivalueconverter but cant figure out how to pass the current object in as one of the 'values'. I'm able to pass the string "Images" but the current Entity that is being operated on i cant seem to pass to the method. The standard {Binding Path =/} doesnt seem to work. thanks again
Without Me It Just Aweso
You need to post some code to show what you want to databind, we can't know what an Entity is.
Wallstreet Programmer
updated my origial post with new code . thanks!
Without Me It Just Aweso
thank you for the updated post. The code provided does work but I'm wanting to figure out the ValueConverter answer also. <Binding . /> doesnt build. The datacontext of the textblock is an Entity. thanks!
Without Me It Just Aweso
Try <Binding Path="." />
Wallstreet Programmer
thanks, through messing around I found it is <Binding /> I appreceiate all your help!
Without Me It Just Aweso