none
How to evaluate a PropertyPath?

    Question

  • Hi!

    Is there any possibility in WPF to evaluate a PropertyPath?

    For example, I want to be able to do this:

    PropertyPath path = new PropertyPath("Top");  
    Rect rect = new Rect(1, 2, 3, 4);  
    object o = EvaluatePropertyPath(path, rect); 

    "o" then should have the value "2" (the "top" of the rectangle).

    I need this feature because I want to write a custom control that can be used like this:

    <x:MyControl ItemsSource="{Binding ...}" 
       Path1="... property path ..." 
       Path2="... property path ..." /> 

    I.e., it shall be possible to bind the custom control to a data collection and extract certain values for each item in the collection using Path1 and Path2.

    Or is there another way to implement this kind of data binding? (Please note that I cannot use DataTemplate in my custom control, because the user of the control shall not be able to change the look and feel of the control.)

    Regards
    - Frank

    Friday, July 25, 2008 9:54 AM

Answers

  • -> I.e. there should be some way to evaluate the binding using a context. Setting FrameworkElement.DataContext before retrieving Path1's value does not work; I already tried this.

    I have to say that data binding is not designed for the specific purpose as you describe above, you'd better loop through the data bound source object instead, rather than looping though the data bound target object.

    Hope this clears things up a little bit.
    • Marked as answer by Marco Zhou Thursday, July 31, 2008 10:14 AM
    Wednesday, July 30, 2008 4:10 AM

All replies

  • How about defining those Path1 and Path2 properties as dependency properties, so that you can directly bind data to them?

    Thanks
    Tuesday, July 29, 2008 8:34 AM
  • How would you then evaluate Path1 and Path2 ?
    For example, suppose that ItemsSource is bound to an array of Rects like this:

    new Rect[]{  
       new Rect(1, 2, 3, 4),  
       new Rect(10, 20, 30, 40),  
       new Rect(100, 200, 300, 400)  
    }  
     
    If Path1 were {Binding Path=Left} and Path2 were {Binding Path=Top}, then my control internally should be able to perform a loop that returns 1, 10, and 100 for Path1 and 2, 20, 200 for Path2.

    I.e. there should be some way to evaluate the binding using a context. Setting FrameworkElement.DataContext before retrieving Path1's value does not work; I already tried this.

    Regards
    - Frank
    Tuesday, July 29, 2008 9:31 AM
  • -> I.e. there should be some way to evaluate the binding using a context. Setting FrameworkElement.DataContext before retrieving Path1's value does not work; I already tried this.

    I have to say that data binding is not designed for the specific purpose as you describe above, you'd better loop through the data bound source object instead, rather than looping though the data bound target object.

    Hope this clears things up a little bit.
    • Marked as answer by Marco Zhou Thursday, July 31, 2008 10:14 AM
    Wednesday, July 30, 2008 4:10 AM