4

I only have a few months experience with XAML and often have a difficult time determining how to bind to or reference elements defined in various places in my XAML. I recently ran across X:Reference, which I understand is new with XAML 2009 and .NET 4.

For example, assume I am trying to bind to the IsChecked property of an element named DisplayIndicator, which is defined elsewhere in my XAML. X:Reference allows me to do it as follows:

{Binding Source={x:Reference DisplayIndicator}, Path=IsChecked}

This seems like an easy way (and preferred way?) to reference almost any element declared in my XAML. Am I wrong about this? Is there a downside to using this?

Thanks very much.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
Randy Minder
  • 47,200
  • 49
  • 204
  • 358

2 Answers2

4

From the x:Reference documentation:

In WPF and XAML 2006, element references are addressed by the framework-level feature of ElementName binding. For most WPF applications and scenarios, ElementName binding should still be used. Exceptions to this general guidance might include cases where there are data context or other scoping considerations that make data binding impractical and where markup compilation is not involved.

I also only use x:Reference if ElementName is not an option. One thing left unmentioned here is that x:Reference is rather picky with cyclical dependencies which is not the case for ElementName; so that would be a downside.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Good point that x:Reference doesn't like cyclic dependencies. I have two UI elements that bind to properties on each other - ElementName is ok with that but x:Reference fails. ElementName however can be problematic when reloading a view - the bindings can fire before the visual tree is available and so the bindings fail but x:Reference seems to be ok in this situation as it doesn't require the visual tree to be available. – The Lonely Coder Oct 07 '14 at 09:21
3

Unfortunately, you cannot use XAML 2009 features with XAML that is used to define visual elements in a WPF application. See http://msdn.microsoft.com/en-us/library/ee792007.aspx:

In WPF, you can use XAML 2009 features, but only for XAML that is not WPF markup-compiled. Markup-compiled XAML and the BAML form of XAML do not currently support the XAML 2009 language keywords and features.

But as for x:Reference there is an easy alternative:

{Binding ElementName=DisplayIndicator, Path=IsChecked}
Pavlo Glazkov
  • 20,498
  • 3
  • 58
  • 71
  • 1
    I am currently using X:Reference in a WPF application using XAML to define visual elements. And it works quite well. On the other hand, your easy alternative isn't so easy. I was trying to use that exact syntax and was getting binding errors (VS Output window) at runtime telling me it couldn't find the element name. This is what led me to X:Reference. – Randy Minder Feb 03 '12 at 20:50
  • @RandyMinder - Well, it might work, but officially it is not supported. One more link: http://msdn.microsoft.com/en-us/library/ee795380.aspx. It is strange that you got binding errors when used `ElementName`, it should work pretty much the same. What did the errors say? – Pavlo Glazkov Feb 03 '12 at 21:06
  • 1
    When I use ElementName instead of X:Reference, I get the following error: "Cannot find source for binding with reference 'ElementName=DisplayIndicator'. BindingExpression:Path=IsChecked; DataItem=null; target element is 'DataSeries' (Name=''); target property is 'MarkerEnabled' (type 'Nullable`1')" – Randy Minder Feb 03 '12 at 21:19
  • 1
    @PavloGlazkov: They do **not** work the same, element name relies on name scopes and possibly other things, it is less versatile than `x:Reference` – H.B. Feb 03 '12 at 21:40