3

Is there any way of turning a string containing a binding expression into a Binding object?

A simple example would be "{Binding Path}",

or:

"{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=views:IPage}, Path=TensileTestChange}"

The actual binding expression is stored in XML, which is a higher-level representation of a XAML document.

6
  • You could look into the Binding constructor that takes a string Commented Jun 18, 2013 at 7:38
  • Hi @Trustme-I'maDoctor, I've expanded with another example I need to implement Commented Jun 18, 2013 at 7:46
  • I have never actually used raw Binding before, so the best I can personally do is just refer you to the Binding class, which holds all the properties you need - in fact it's the same binding that's used in XAML. I don't know if you can just pass it a predefined string somehow, or if you'd have to parse the string first (which would be counterproductive). Commented Jun 18, 2013 at 7:49
  • Thank you. Unfortunately I already use the raw binding class. I was wondering in my question whether there was a ready made solution to parse a binding expression and return a Binding object Commented Jun 18, 2013 at 7:51
  • Maybe XamlReader would be of help here then...? Commented Jun 18, 2013 at 7:53

1 Answer 1

4

What you are asking is to parse a MarkupExtension. I could not find WPF's implementation directly (it's contained somewhere in the XamlReader.Parse call chain).

After some googling, it seems there is no ready made solution available to do this. However, if you have some experience with writing parsers you could roll your own. The specification for parsing a MarkupExtension is given on MSDN.

As a workaround, you could fake a control where you put the binding on:

string myBindingExpression = "{Binding MyProperty}";

var test = "<TextBlock xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" Text=\"" 
    + myBindingExpression + "\" />";
var result = XamlReader.Parse(test) as TextBlock;
var bindingExpression = result.GetBindingExpression(TextBlock.TextProperty);

Binding binding = bindingExpression.ParentBinding

This creates a TextBlock with the binding as the Text property. It will give you the binding object with the properties set according to the binding expression.

You can then apply the binding wherever.

Remember though, for your more complex example with the xmlns prefix, you need to include the xmlns:views="..." in the fake TextBlock, otherwise it will not know what to do with the prefix.

Example: <TextBlock xmlns:views="..." xmlns="..." Text="{Binding MyProperty}" />

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Bas, good to know it's called a MarkupExtension, I couldn't find the correct term for it! I'll keep in mind the comment about the xmlns as well, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.