DCSIMG
WPF BindingEx – Runtime resolved Path - Essential WPF

WPF BindingEx – Runtime resolved Path

One of my blog readers sent me an email regards how to create Binding to an object via XAML, where Path is unknown at design time.

For example, you want to bind to Source.Property property, where Property is provided by another property.

public partial class Window1 : Window

{

    public Window1()

    {

        Path = "Name";

 

        Person = new Person()

        {

            Name = "Tomer Shamam"

        };                      

 

        DataContext = this;

 

        InitializeComponent();

    }

 

    public string Path { get; private set; }

    public Person Person { get; private set; }

 

...

}

<TextBox Text="{Binding Path=???}" />

As you may guess correctly, we can’t bind to dynamic resolved path, unless you create the binding from code and explicitly specify the path (which is not always possible, and not recommended by design).

To solve this problem, I’ve just created a custom Binding object called BindingEx. BindingEx has two new properties: SourcePath and PathOrigin.

SourcePath – The path to the source object.

PathOrigin – The path to the dynamic resolved path.

Using BindingEx the markup above should be look like this:

<TextBox Text="{t:BindingEx SourcePath=Person, PathOrigin=Path}" />

As you can see, the Window1 instance is in the DataContext so we don’t have to specify source (but we can if we want to). SourcePath indicates that the binding should look for the source object in the Person property (which belongs to Window1 in this case), and PathOrigin points to the Path property (which also belongs to Window1 instance). This is where binding should extract the path to be bound to.

You may download my custom binding from here. Note that I didn’t check all the possible cases (there are many), so use it at your own risk.

Published Wednesday, August 05, 2009 12:34 PM by Tomer Shamam

Comments

# re: WPF BindingEx – Runtime resolved Path

Wednesday, August 05, 2009 11:33 PM by Tamir Dresher

Thank you very much for the post.

How can i use to take the SourcePath from another element?

lets say i want the to take the SourcePath from a TextBox.

i cant use binding on SourcePath so i tried to make the Windows' Path a dependency property and bind the TextBox to it but the changes i do at runtime doesnt reflect on the place i used the BindingEx.

Am i doing anything wrong?

# re: WPF BindingEx – Runtime resolved Path

Saturday, August 08, 2009 9:48 PM by Tomer Shamam

Hi Tamir,

Unfortunately you can't use BindingEx pick the SourcePath from a property value. The SourcePath must be one level less as if it was the standard Path property.

For example, if you have a TextBox and you want to bind its property you must provide the path to reach the TextBox, then you can use PathOrigin to pick the path.

Note that you can change it a bit to have such functionality.

You already have my code, Good Luck! :-)

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above:
Powered by Community Server (Commercial Edition), by Telligent Systems